1

Hello I do the PHP Upload multiple files, but my form does not require to upload all field (require just at least one field upload) so user may not upload the second input field.

The problem is if user upload only 1 file, user will get the error "Invalid file" (final else statement). But if user upload all 2 fields, it does not have error, I guess the error come from null upload field. So I use if not empty first, but it's not work. How should I do?

<form action="upload.php"  method="post" enctype="multipart/form-data">         
            Image 1 :
                <input type="file" name="images[]" />
                <br>   
            image 2 :
                <input type="file" name="images[]" />  <br>

                <input type="submit" value="Upload" />
</form>

in upload.php page

if(!empty($_FILES))
{
      $count = count($_FILES["images"]["name"]);

    for($i=1; $i <= $count; $i++)
    {   
        if ((($_FILES["images"]["type"][$i-1] == "image/gif")
             || ($_FILES["images"]["type"][$i-1]  == "image/jpeg")
            || ($_FILES["images"]["type"][$i-1]  == "image/png"))
            && ($_FILES["images"]["size"][$i-1] < 2000000)) //2 MB
        {

             if ($_FILES["images"]["error"][$i-1]  > 0)
             {
                 echo "File Error : " . $_FILES["images"]["error"][$i]  . "<br />";
             }
             else 
             {
                if (file_exists("path/to/".$_FILES["images"]["name"][$i-1] ))
                {
                   echo "<b>".$_FILES["images"]["name"][$i-1]  . " already exists. </b>";
                }
                else
                {
                   $newname = date('Y-m-d')."_".$i.".jpg";
                   move_uploaded_file($_FILES["images"]["tmp_name"][$i-1] , "path/to/".$newname);

                 }
               }
             }
                else
                {
                   echo "Invalid file" ;
                   exit();
                } 
          }
       } 

1 Answer 1

2

You can change your first line to:

if(isset($_FILES["images"]["name"][0])) {

With one or multiple files this can work

on line with:

echo "Invalid file" ;
exit();

this can be a problem, because if the first file have a problem you exit aplication and not process the second or others.

i suggest to you this code, too:

<?php 
if (isset($_FILES["images"]["name"][0])) {
$path_upload = 'path/upload/';
$count = count($_FILES["images"]["name"]);
$allowed_types = array("image/gif", "image/jpeg", "image/png");
$nl = PHP_EOL.'<br>'; // Linux: \n<br> Window: \r\n<br>

for($i=0; $i < $count; $i++)
{   
    $file_type = $_FILES["images"]['type'][$i];
    $file_name = $_FILES["images"]['name'][$i];
    $file_size = $_FILES["images"]['size'][$i];
    $file_error = $_FILES["images"]['error'][$i];
    $file_tmp = $_FILES["images"]['tmp_name'][$i];

    if (in_array($file_type, $allowed_types) && $file_size < 2000000) {

        if ($file_error > 0) {

            echo 'Upload file:'.$file_name.' error: '.$file_error.$nl;

        } else {

            $path_new_upload = $path_upload.$file_name;

            // verify while filename exists
            while (file_exists($path_new_upload)) {

                $ext = explode('.', $file_name); // explode dots on filename
                $ext = end($ext); // get last item of array
                $newname = date('Y-m-d_').$i.'.'.$ext;

                $path_new_upload = $path_upload.$newname;

            }

            move_uploaded_file($file_tmp, $path_new_upload);

        }


    } else {

        echo 'Invalid file type to: '.$file_name.$nl;
        // continue the code
    }
}

}

Can be better, this verify filename to new creation.

Sign up to request clarification or add additional context in comments.

Comments

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.