Jump to content
Sign in to follow this  
imgrooot

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

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?

 

 

Share this post


Link to post
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") {}

Share this post


Link to post
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.

Edited by benanamen

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • 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.