Multiple File Upload PHP

2019-07-16 08:16发布

问题:

I use the Following code to upload a single file .With This code I can Upload a single file to the database .I want to make multiple files uploaded by selecting multiple files in a single input type file.What Changes should i make in the code to make it upload multiple files?

<?PHP
INCLUDE ("DB_Config.php");
$id=$_POST['id'];
$fileTypes = array('txt','doc','docx','ppt','pptx','pdf');
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if(in_array($fileParts['extension'],$fileTypes))
{       
    $filename = $_FILES["uploaded_file"]["name"];
    $location = "E:\\test_TrainingMaterial/";
    $file_size = $_FILES["uploaded_file"]["size"];
    $path = $location . basename( $_FILES['uploaded_file']['name']);
    if(file_exists($path))
    {
        echo "File Already Exists.<br/>";
        echo "Please Rename and Try Again";
    }
    else
    {
        if($file_size < 209715200)
        {   
            $move = move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
            $result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."')");
            if ($result) 
            {
                do {
                    if ($temp_resource = $mysqli->use_result()) 
                    {
                        while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
                            array_push($rows, $row);
                        }
                        $temp_resource->free_result();
                    }
                } while ($mysqli->next_result());
            }
            if($move)
            {
                echo "Successfully Uploaded";
            }
            else
            {
                echo "File not Moved";
            }
        }
        else
        {
            echo "File Size Exceeded";
        }
    }
}
else
{
    echo " Invalid File Type";
}
?>

The Html That is used is

<form id = "upload_form" method="post" enctype="multipart/form-data"  >
    <input type="file" name="uploaded_file" id="uploaded_file" style="color:black" /><br/>
</form>

回答1:

Basically you need to add to input name [] brackets and attribute "multiple"

<form id = "upload_form" method="post" enctype="multipart/form-data"  >
    <input type="file" name="uploaded_file[]" multiple="true" id="uploaded_file" style="color:black" /><br/>
</form>

Now all uploaded file will be available via

$_FILES['uploaded_file']['name'][0]
$_FILES['uploaded_file']['name'][1]

and so on

More info at http://www.php.net/manual/en/features.file-upload.multiple.php