多文件上传PHP [英] Multiple File Upload PHP

查看:85
本文介绍了多文件上传PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

使用的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>

推荐答案

基本上,您需要在输入名称[]括号和属性"multiple"中添加

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]

以此类推

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

这篇关于多文件上传PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆