tags:

views:

56

answers:

3

Here is a problem. I have an HTML form with several fields in it. One of the fields - 'Upload file'. When I upload a file, everything works properly. But when I choose to submit the form without a file, it gives me the error message: "There was an error uploading the file, please try again". Looks to me that the script thinks that uploading a file is mandatory. How do I change it?

Here is my PHP:

//File upload

// Where the file is going to be placed 
$target_path = "uploads/";

// Add the original filename to our target path.  
//Result is "uploads/filename.extension" 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}   

//End of file upload

Thank you!

+2  A: 

You should check using the function is_uploaded_file

try adding the following condition before calling the function move_uploaded_file

if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
dejavu
This worked, thank you! Now, though, there is another problem. I want the file to be deleted in the directory after it was sent as an attachment with an email. When there is a file uploaded, it all works fine. But if there isn't, I get this error message: Warning: unlink(uploads/) [function.unlink]: Is a directory in /mydomain.com/Hawaii/html/contact_email.php on line 191 How do I fix it?
vlevsha
the parameter of the function unlink must be a file .. in your case you passed 'uploads/' which is a folder.
dejavu
How do I define the file?I tried a few things, and they didn't work.Also, when I use $target_path, the file actually gets deleted from the folder, whereas when I use a file variable, it doesn't.I thought, maybe I should do a check similar to this:if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
vlevsha
Where did you exactly put the unlink function call ?
dejavu
Right after the email is sent://Send the message$numSent = $mailer->send($message);printf("Sent %d messages\n", $numSent);unlink($target_path);
vlevsha
Also, try using the function is_file before calling unlink.
dejavu
A: 

move_uploaded_file returns a bool, true or false, depending on the success of the operation.

A solution would be to check more rigorously, e.g. if a file was uploaded at all via is_uploaded_file function.

For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the clients machine $_FILES['userfile']['name'] does not work.

The MYYN
This worked, thank you! Now, though, there is another problem.I want the file to be deleted in the directory after it was sent as an attachment with an email.When there is a file uploaded, it all works fine.But if there isn't, I get this error message:Warning: unlink(uploads/) [function.unlink]: Is a directory in /mydomain.com/Hawaii/html/contact_email.php on line 191 How do I fix it?
vlevsha
The MYYN
A: 

The target path of move_uploaded_files should be a folder, not a file.

You should also check if the folder is_writeable and is_dir.

corrodedmonkee