0

I am trying to upload multiple images to server and mysql database, however the first image out of the multiple images is not being uploaded.

I iterate through the selected files and trying to upload each image in a for loop. All the images upload correctly except the first image.

Here is my html to select multiple files:

<form action="imageUpload.php" method="post" enctype="multipart/form-data">
Select Image File to Upload:
<input type="file" name="files[]" multiple>
<input type="submit" name="submit" value="Upload">

My php codes is as follows:

<?php
include("php/db.php");
$statusMsg = '';

// Count # of uploaded files in array
$total = count($_FILES["files"]["name"]);

// File upload path
$targetDir = "productImages/";

if(isset($_POST["submit"]) && !empty($_FILES["files"]["name"]))
{   
for( $i=0 ; $i < $total ; $i++ )
{                   
    // Allow certain file formats
    $allowTypes = array('JPG','jpg','png','jpeg','gif');
    $fileName = basename($_FILES["files"]["name"][$i]);
    $targetFilePath = $targetDir . $fileName;
    $fileType = strtolower(pathinfo($targetFilePath,PATHINFO_EXTENSION));       

    if(in_array($fileType, $allowTypes))
    {
        // upload file to temporary location
        if (is_uploaded_file($_FILES["files"]["tmp_name"][$i])) 
        {
            // Upload file to server
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$i], $targetFilePath))
            {

                // Insert image file name into database
                $insertSQL = "INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())";         

                if(openConnection())
                {
                    $insert = insertQuery($insertSQL);      
                }           
                closeConnection();

                if($insert){
                    $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
                }else{
                    $statusMsg = "File upload failed, please try again.";
                } 
            }
            else
            {
                $statusMsg = "Sorry, there was an error uploading your file.";
            }
        }
        else
        {
            $statusMsg = "Sorry, file could not upload to temp location.";
        }

    }
        else
        {
            $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF files are allowed to upload.';
        }
}

}else{
$statusMsg = 'Please select a file to upload.';
}

echo "<br>" . $statusMsg;
?>

Can anyone please identify what is the issue?

shaikh
  • 582
  • 6
  • 24

2 Answers2

0

You are counting a property that does not exist. You are counting "name" of "files" in $FILES. $FILES is an associative array that may contain arrays. In your case, "files" is an array. So try to count that instead.

$total = count($FILES["files"])
Seaskyways
  • 3,630
  • 3
  • 26
  • 43
  • When uploading multiple files, `$_FILES['files']['name']` will be an array. See the [manual](http://php.net/manual/en/features.file-upload.post-method.php) – Nick Dec 23 '18 at 04:55
  • $_FILES['files']['name'] is an array. $total = count($FILES["files"]) is giving me correct files count. – shaikh Dec 23 '18 at 05:02
  • Please post the result of ``print_r($_FILES);`` so we can see what is being uploaded exactly. – kmoser Dec 23 '18 at 05:43
0

I found the problem. The problem was not with the code but with the "php.ini" file of my XAMPP. The files I was trying to upload were greater than 2MB, whereas the max file size that can be uploaded was set to 2MB in the "php.ini" file.

I followed the following link to set the following fields in the php.ini file:

upload_max_filesize = 200M
post_max_size = 201M

source: upload large file in php

shaikh
  • 582
  • 6
  • 24