0

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!

3 Answers 3

1

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'])) {
Sign up to request clarification or add additional context in comments.

6 Comments

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?
the parameter of the function unlink must be a file .. in your case you passed 'uploads/' which is a folder.
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'])) {
Where did you exactly put the unlink function call ?
Right after the email is sent: //Send the message $numSent = $mailer->send($message); printf("Sent %d messages\n", $numSent); unlink($target_path);
|
0

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.

Comments

0

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.

2 Comments

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?
If these posts fixed this problem, accept one answer and ask another question with your successive issue. This is preferred over Q&A spirals through comments and edits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.