Jump to content

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


imgrooot
Go to solution Solved by 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

  • Solution

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.

Edited by benanamen
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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