通过php上传多个文件

时间:2017-09-13 06:10:36

标签: php mysql database

    <form action="" method="post" enctype="multipart/form-data">
    <input type="" name="fname" />
    <input type="file"  name="filepath" id="filepath"/><br><br>
    <input type="submit" name="SubmitButton" value="Submit"/>
    </form>

 -----------------php-----------------------------------   
    $newname=$_POST['fname'];
    $target_dir = 'uploads/reports/';
    $filename=$_FILES["filepath"]['name'];
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    $_FILES["filepath"]['name']=$newname.".".$ext;

    $target_file = $target_dir . basename($_FILES["filepath"]['name']);
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $_FILES["tmp_name"]=$newname;
    echo $target_file;
    move_uploaded_file($_FILES["filepath"]["tmp_name"], $target_file);

    } 

我需要从单个输入上传多个文件(excel,word文档,图像)(我的意思是,我们可以在浏览时选择多个文件)到数据库,此代码仅适用于一个文件。并将该文件重命名为$ newnam(1),$ newname(2)....喜欢那个

1 个答案:

答案 0 :(得分:0)

为此,您必须将输入名称作为数组提供,并提供multiple属性,如:

<input type="file"  name="filepath[]" id="filepath" multiple />

您可以使用以下方式获取其内容:

$_FILES['filepath']  // It returns an array