Jump to content

Recommended Posts

Hey

 

I am working on an image upload for my site but I am having an issue at the moment, I am allowing multiple files from the one input box which works fine, it uploads all the files I select and I can echo out each files information on the next page. However I want to filter file types by images but I am having an issue.

 

I found an example of using in_array to check the file type against the acceptable file types that are stored in array.

 

Function UploadFiles($imgs, $UserID, $DT, $IP) {
    //Setup the variables.
    $MaxFileSize = 6291456;
    $Allowed_FileTypes = array("image/jpeg","image/jpg","image/gif","image/bmp","image/png");
    
    $FileCount = count($_FILES['imgs']);
    //Now the uploads have been submitted and uploaded properly we can process them.
    for ($i = 0; $i < $FileCount; $i++) {
        $FileName = $_FILES['imgs']['name'][$i];
        $FileSize = $_FILES['imgs']['size'][$i];
        $FileType = $_FILES['imgs']['type'][$i];

if(in_array($FileType, $Allowed_FileTypes)) {
  $Result = "Uploaded";
} else {
  $Result = "<div id='error1'>Error: That File Type isn't supported, You can only upload JPG, PNG, GIF and BMP files.</div><br />";
}
        
    }
    return $Result;
  }

 

Now if I echo the $FileType out it displays it as image/jpeg etc but it always displays the error.

 

Any ideas or will this just not work this way?

Link to comment
https://forums.phpfreaks.com/topic/242530-multiple-file-upload-file-type-problem/
Share on other sites

Yes sorry, should have put that in my first post as well.

 

<form action="./upload.php" method="post" enctype="multipart/form-data" class='UploadForm'>
<label for='imgs'>Image(s):</label> <input type='file' name='imgs[]' multiple />
<br />
<input type='submit' name='Upload' value='Upload' /></form>

 

It's nice and simple for now. That was the example I found on the net showing how to do multiple files, like I said it seems to be uploading them fine because I can echo out the $FileType or $FileName and it outputs the correct information.

You MUST test for errors before you can access ANY of the uploaded file information. See Example #3 at the following link for how you can iterate over any number of multiple uploaded files - http://is.php.net/manual/en/features.file-upload.post-method.php

 

You would put your code that accesses the uploaded file information inside the if ($error == UPLOAD_ERR_OK) { code to access the successful uploaded file information } statement.

 

Also, when validating user supplied information, you should tell the user in the error message that you output exactly what was wrong with the data he supplied. You should echo the $FileType value as part of your error message so that you have debugging information during testing (different browsers and browser versions supply different type values for the same file) and a user has more information about what he actually submitted that was incorrect.

$FileCount = count($_FILES['imgs']);

This will always be 5 if two or more files are uploaded. You'll want to count $_FILES['imgs']['name'] or some other member.

 

Can't figure out why in_array might not be matching though...

 

The count always being 5 was actually the problem :) Of course it checked the first one which was actually an image and that was correct and matched, but because I wasn't storing each error message up it was checking the other 4 which thrown errors. Counting it by the file names makes it work as it should.

 

You MUST test for errors before you can access ANY of the uploaded file information. See Example #3 at the following link for how you can iterate over any number of multiple uploaded files - http://is.php.net/manual/en/features.file-upload.post-method.php

 

You would put your code that accesses the uploaded file information inside the if ($error == UPLOAD_ERR_OK) { code to access the successful uploaded file information } statement.

 

Also, when validating user supplied information, you should tell the user in the error message that you output exactly what was wrong with the data he supplied. You should echo the $FileType value as part of your error message so that you have debugging information during testing (different browsers and browser versions supply different type values for the same file) and a user has more information about what he actually submitted that was incorrect.

 

Thanks for the tips, I will make sure I check for errors with the images as well.

 

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.