0

Hi guys I'm using this code http://www.w3schools.com/php/php_file_upload.asp to upload files and it works great but now I'm doing a form with an optional image and it won't let me upload. It says it's an invalid file format since it's no file at all. How do I check if there is a file to upload or not and then skip the upload script if there is none?

Thanks!

Note: I have removed the size limitation from the script if that makes any differance.

3
  • Well it has some good info for beginners, but otherwise i kinda agree with these guys: w3fools.com Commented Aug 15, 2011 at 15:32
  • Haha, I guess it't not the most advanced stuff in the world but I didn't understand anything on the other sites I found so... Commented Aug 15, 2011 at 15:36
  • BTW Thanks for the heads up cuz I thought was a part af W3C just becouse of it's name. Commented Aug 15, 2011 at 15:52

2 Answers 2

2
if ($_FILES['nameofyourfileinput']['error'] === UPLOAD_ERR_OK) {
   ... file has been uploaded and everything went ok ...
} else if ($_FILES['nameofyourfieinput']['error'] !== UPLOAD_ERR_NO_FILE) {
   ... something OTHER than a 'no file was uploaded' error has occured...
}

Upload error codes defined here.

Sign up to request clarification or add additional context in comments.

2 Comments

What does three = in a row mean?
php.net/manual/en/language.operators.comparison.php It's the "strict comparison". compares type AND value. PHP evaluates 0 == false as true because false will convert to 0 (or 0 to false). saying 0 === false will fail, because the types of the two are different, even if the values come out the same.
0

Just check if the file's error code isnt 4; http://php.net/manual/en/features.file-upload.errors.php

Comments

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.