Jump to content

$_FILES type with a conditional statement

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
crippled

crippled

    Newbie

  • Members
  • PipPip
  • 12 posts
Hello,

I am going crazy over why this if block is executing when I try and upload a valid .gif file.

        $screenshot_type = $_FILES['screenshot']['type'];
        
        // check if the screenshot type is supported (jpeg, png or gif)
        if ($screenshot_type != NULL && ($screenshot_type != 'image/jpeg'
            || $screenshot_type != 'image/pjpeg' || $screenshot_type != 'image/gif'
            || $screenshot_type != 'image/png')) {
                echo '<p class="error">Invalid file type. Supported file types are '
                    . 'JPEG, GIF or PNG.</p>';
        }

even if I do an echo $screenshot_type; I get 'image/gif', so why in the world is the above conditional being executed?

#2
crippled

crippled

    Newbie

  • Members
  • PipPip
  • 12 posts
After going over this for some time now, I realize that $screenshot_type ($_FILES['screenshot']['type']) is being set to NULL if the filesize is over the limit specified by the forms MAX_FILE_SIZE attribute.

I have yet to figure out the correct logic of displaying a specific error message. Here is the deal..

I have a form that accepts a name, score and screenshot file. I want the form to report a SPECIFIC error when it occurs. The errors are

  • form field is missing,
  • screenshot file is NOT an image file,
  • screenshot file is too big.
This would seem easy, but now I have to sneak in the $_FILES['screenshot']['type'] being equal to NULL in some random cases, and $_FILES['screenshot']['error'] being something other than 0 in other cases.

What I have so far tests for the filetype to be jpeg, gif or png. If it isn't, display the error. However, if I try and upload a jpg file OVER the filesize limit, it gives the wrong error because the filetype is NULL, even though it's a JPEG!


:mad:

It seems no matter what I do, I can't catch the exact error. For example, if I try and upload a pdf file larger than the given limit I get the file size error, even though I try and catch the unsupported file types before file size.

Edited by crippled, 22 June 2010 - 02:14 PM.


#3
leeyo

leeyo

    Newbie

  • Members
  • PipPip
  • 12 posts
$screenshot_type != NULL ...
wtf...

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I'm a little tired so I don't see any straightout errors, but try to start from the ground up:
Make sure the <form> element starts as so:
<form action="???.php" method="post" enctype="multipart/form-data">
Then for testing, why not dump the $_FILES['screenshot'] element?:
<?php
    print "<pre>";
    print_r($_FILES['screenshot']);
?>
This should help you debug problems, keep it up while you're writing your script, as you can attribute each problem to the actual facts.

As for the way you structured your IF statements, I'm not sure if it'd work or not, try something basic such as:
//One megabyte
$MAX_FILE_SIZE = 1048576; 

//Allowed Mimetypes
$allowedTypes = Array("image/jpeg", "image/pjpeg", "image/gif", "image/png");

//Check for upload errors:
if ($_FILES["screenshot"]["errors"] > 0) {
    print '<p class="error">Problem uploading image file, please try again later.</p>';
}

//Check filesize
if($_FILES["screenshot"]["size"] > $MAX_FILE_SIZE) { 
    print '<p class="error">File size too big, Please choose a smaller file.</p>';
}

//Check finally the mimetypes:
if(!in_array($_FILES['screenshot']['type'], $allowedTypes)) {
    print '<p class="error">Invalid file type. Supported file types are '
          . 'JPEG, GIF or PNG.</p>';
}
Should be cleaner and that should allow better insight into what is happening, add the print_r statement if you wish to debug of course with this code.

::EDIT::::

As per your original code; you used OR (||) in your statements, which would mean if it was gif, it still would not be jpeg or png, so the OR statement should be AND (&&).

Glad coffee helped me. :P
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
crippled

crippled

    Newbie

  • Members
  • PipPip
  • 12 posts
NullW0rm,

I can't thank you enough for your response. The way you put it makes sense, I will play with the $_FILES array and restructure my conditional statements. Thanks again!

:cool: