uploading a user picture to the website

Hello All:

I am trying to come up with a way for a website user to upload a jpg from his computer to a “parking lot” on the site (customerimages/). the code i am using is as follows:
[php]

<?php // define a constant for the maximum upload size define ('MAX_FILE_SIZE', 1024 * 50); if (array_key_exists('upload', $_POST)) { // define constant for upload folder define('UPLOAD_DIR', 'customerimages/'); // replace any spaces in original filename with underscores $file = str_replace(' ', '_', $_FILES['image']['name']); // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // upload if file is OK if (in_array($_FILES['image']['type'], $permitted) && $_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) { switch($_FILES['image']['error']) { case 0: // check if a file of the same name has been uploaded if (!file_exists(UPLOAD_DIR . $file)) { // move the file to the upload folder and rename it $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR . $file); } else { $result = 'A file of the same name already exists.'; } if ($success) { $result = "$file uploaded successfully."; } else { $result = "Error uploading $file. Please try again."; } break; case 3: case 6: case 7: case 8: $result = "Error uploading $file. Please try again."; break; case 4: $result = "You didn't select a file to be uploaded."; } } else { $result = "$file is either too big or not an image."; } } ?> Untitled Document

Upload image:

[/php]

The code does place the upload on the screen, allow the user to browse for a pic, and when upload is selected, it APPEARS to work but we are not getting the image into the customerimages folder on the server. Any ideas?

Thanks, Kevin

If you echo $result, what does it say?

Remove the “DEFINE” code and change that to a variable. Define assigns a text value to another text value.
So, this line:
define(‘UPLOAD_DIR’, ‘customerimages/’);
Assigns a text value to a text name. And, that means that this line:
if (!file_exists(UPLOAD_DIR . $file)) {
Becomes:
if (!file_exists(customerimages/ . $file)) {
Basically, that is an invalid statement… And, this line:
move_uploaded_file($_FILES[‘image’][‘tmp_name’], UPLOAD_DIR . $file);
Becomes:
move_uploaded_file($_FILES[‘image’][‘tmp_name’], customerimages/ . $file);
Another invalid line… (no quotes around the folder name!)…

I may be wrong, but, I think that is your error… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service