Jump to content

Not recognizing file type


9three

Recommended Posts

Hey,

 

I'm creating an uploader and for some reason when I add to check other mime type files it doesnt recognize it properly.

 

  if ($_FILES['file']['type'] != 'image/jpeg' || $_FILES['file']['type'] != 'image/png' || $_FILES['file']['type'] != 'image/gif') {
    echo '
    <script type="text/javascript">
    parent.document.getElementById("uploadTxt").innerHTML = "That file extension is not allowed: '.$_FILES['file']['type'].' Please try again."
    </script>';
    return false;
  }

 

This doesn't work. But if I remove

|| $_FILES['file']['type'] != 'image/png' || $_FILES['file']['type'] != 'image/gif'

 

And just leave != 'image/jpeg' it works correctly. It's confusing me?  :shrug:

Link to comment
Share on other sites

see whats going in and confirm its not your fault

echo $_FILES['file']['type'];

if ($_FILES['file']['type'] != 'image/jpeg' || $_FILES['file']['type'] != 'image/png' || $_FILES['file']['type'] != 'image/gif') {
    echo '
    <script type="text/javascript">
    parent.document.getElementById("uploadTxt").innerHTML = "That file extension is not allowed: '.$_FILES['file']['type'].' Please try again."
    </script>';
    return false;
  }

Link to comment
Share on other sites

I'm already echoing it..

 

parent.document.getElementById("uploadTxt").innerHTML = "That file extension is not allowed: '.$_FILES['file']['type'].' Please try again."

 

I've tried 2 files:

 

a jpg and a jpeg

 

jpg works

 

jpeg does not

 

Output: That file extension is not allowed: image/jpeg Please try again.

Link to comment
Share on other sites

Think about the logic of

if ($_FILES['file']['type'] != 'image/jpeg' || $_FILES['file']['type'] != 'image/png' || $_FILES['file']['type'] != 'image/gif') {

Upload a gif file... first test ($_FILES['file']['type'] != 'image/jpeg') gives true. No further checking is done because you're using the || operator. Therefore, the whole "if" is true and the file is rejected.

 

Try using && instead of ||

Link to comment
Share on other sites

Think about the logic of

if ($_FILES['file']['type'] != 'image/jpeg' || $_FILES['file']['type'] != 'image/png' || $_FILES['file']['type'] != 'image/gif') {

Upload a gif file... first test ($_FILES['file']['type'] != 'image/jpeg') gives true. No further checking is done because you're using the || operator. Therefore, the whole "if" is true and the file is rejected.

 

Try using && instead of ||

 

those ones are hard to spot, i think you may have it there, well spotted

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.