0

Script below is not working. Not getting any php error messages. Had asked a similar question, changed around thumbnail creation and if I had updated that question it would not have been seen at all.

Code is not getting past file type check. The input type of file has a name of multifile[].

if ((($_FILES['multifile']['type'][$key] == "image/png") || 
        ($_FILES['multifile']['type'][$key] == "image/jpeg") || 
        ($_FILES['multifile']['type'][$key] == "image/pjpeg") && 
        ($_FILES['multifile']['size'][$key] < 2097152)))
    {

        if(count($_FILES['multifile']['name'])) 
        {
            foreach ($_FILES['multifile']['name'] as $key => $filename)
            {

                $filename = time().'_'.$_FILES['multifile']['name'];  
                $source = $_FILES['multifile']['tmp_name'];  
                $target = 'uploads/'.$filename;  

                if(move_uploaded_file ($_FILES['multifile']['tmp_name'][$key],$target))
                {
                    createThumbnail($filename);  

                    //database insert here

                    echo '<div class="success" style="margin-right:25px;">Images added, view individual album to edit image descriptions</div>';
                }
            }
        }
    }

    else
    {
        echo '<div class="error">Invalid file, only jpg, jpeg, or png file types allowed</div>';
    }

Here is print_r of $_FILES

Array ( [name] => Array ( [0] => image1.png [1] => image2.png ) [type] => Array ( [0] => image/png [1] => image/png ) [tmp_name] => Array ( [0] => /tmp/php1rPxh5 [1] => /tmp/phpsGsA2I ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 27594 [1] => 24466 ) )

1 Answer 1

1

change your if to this

 if ((($_FILES['multifile']['type'][$key] == "image/png") || 
    ($_FILES['multifile']['type'][$key] == "image/jpeg") || 
    ($_FILES['multifile']['type'][$key] == "image/pjpeg")) && 
    ($_FILES['multifile']['size'][$key] < 2097152))

this makes it read as if extension is Ok AND if size is Ok

I personally believe if you are using count then use it this way

if(count($_FILES['multifile']['name']) >0) 

Also if possible can you give output of print_r($_FILES['multifile'])

for($i=0;$i< count($_FILES['multifile']['type']);$i++){
    echo "i:".$i."<br/>";
    if ((($_FILES['multifile']['type'][$i] == "image/png") || 
    ($_FILES['multifile']['type'][$i] == "image/jpeg") || 
    ($_FILES['multifile']['type'][$i] == "image/pjpeg")) && 
    ($_FILES['multifile']['size'][$i] < 2097152))
    {
    echo "icond:".$i."<br/>";

            $filename = time().'_'.$_FILES['multifile']['name'][$i];  
            $target = 'uploads/'.$filename;  

            if(move_uploaded_file ($_FILES['multifile']['tmp_name'][$i],$target))
            {
                createThumbnail($filename);  
                echo '<div class="success" style="margin-right:25px;">Images added, view individual album to edit image descriptions</div>';
            } 
             else
            {
             echo '<div class="error">Invalid file'.$filename.', only jpg, jpeg, or png file types allowed</div>';
             }        
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

can you give print_r result of your $_FILES array
so with my change are you still stuck at first if. where is value of first $key coming from? I can see that you are using $key for iterating foreach ($_FILES['multifile']['name'] as $key => $filename) too
the key is necessary I was curious if it exist at all. Alright One way to troubleshoot is to enter say echo 1;die(0); and move it just under your if condition to see if its getting there and then slowly move it down to see where it is failing. Other thing is to provide full path to variable $target e.g. c:\uploads\filename.png or something like this if uploads folder exits inside the current php file directory $target=dirname(__FILE__)."/uploads/$filename"
It's not even getting past the file type verification.. I tried as you said and no dice.
Any other thoughts of why it's not getting passed the file verification? The single file upload version works just fine with the file type verification.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.