2

我使用以下代码上传单个文件。使用此代码,我可以将单个文件上传到数据库。我想通过在单个输入类型文件中选择多个文件来上传多个文件。我应该在代码中进行哪些更改让它上传多个文件?

<?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";
}
?>

使用的 Html 是

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

1 回答 1

3

基本上你需要添加到输入名称 [] 括号和属性“多个”

<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>

现在所有上传的文件都可以通过

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

等等

更多信息请访问 http://www.php.net/manual/en/features.file-upload.multiple.php

于 2013-02-20T08:48:25.987 回答