Jump to content

Is there a reason why some images won't upload using this script?


imgrooot

Recommended Posts

I am using this script for image uploads.

 

https://www.w3schools.com/php/php_file_upload.asp

 

I noticed that with some of the image uploads, I would get the error 

"Sorry, only JPG, JPEG, PNG & GIF files are allowed."

 

The images i upload are one of the file types listed above.  So I am wondering why i would get an error for some images but not others despite all them of being the same file types?

 

Can you tell why judging from the script?

 

 

Link to comment
Share on other sites

I found what the issue was. 

 

This code in the script has all lowercase extensions.


if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {}

I had to also add all the UPPERCASE extensions as well. And it worked.

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG" && $imageFileType != "GIF") {}
Link to comment
Share on other sites

Doubling the amount of code you are using is not the answer. strtolower is your friend.

 

Checking just the file extension is also not a good idea. A user can put any file extension they want. Doesn't mean it is a valid file type.  You should also be checking the file mime type.

 

Put the extensions in an array and then use in_array to validate. Same with the mime types.

 

w3schools is not the best place to learn from.

Link to comment
Share on other sites

Doubling the amount of code you are using is not the answer. strtolower is your friend.

 

Checking just the file extension is also not a good idea. A user can put any file extension they want. Doesn't mean it is a valid file type.  You should also be checking the file mime type.

 

Put the extensions in an array and then use in_array to validate. Same with the mime types.

 

w3schools is not the best place to learn from.

 

Copy. I will keep that in mind. Thanks.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.