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
https://forums.phpfreaks.com/topic/179498-not-recognizing-file-type/
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;
  }

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.

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 ||

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

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.