为了账号安全,请及时绑定邮箱和手机立即绑定

php中的多文件上传

php中的多文件上传

PHP
梵蒂冈之花 2019-06-12 21:34:26
php中的多文件上传我想上传多个文件并将它们存储在一个文件夹中,获取路径并将其存储在数据库中.任何你想要做多个文件上传的好例子.。注:文件可以是任何类型的.。
查看完整描述

3 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

我知道这是一篇老文章,但对那些试图上传多个文件的人来说,进一步的解释可能会有帮助.下面是你需要做的事情:

  • 输入名称必须定义为数组,即

    name="inputName[]"

  • 输入元素必须具有

    multiple="multiple"

    或者只是

    multiple

  • 在PHP文件中使用语法

    "$_FILES['inputName']['param'][index]"

  • 确保寻找

    空文件名和路径

    ,数组可能包含

    空字符串

    ..使用

    array_filter()

    在数数之前。

下面是一个向下和肮脏的示例(只显示相关代码)

HTML:

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

PHP:

//$files = array_filter($_FILES['upload']['name']); //something like that to be used before processing files.
// Count # of uploaded files in array$total = count($_FILES['upload']['name']);// Loop through each filefor( $i=0 ; $i < $total ; $i++ ) {

  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a file path
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }}

希望这能帮上忙!


查看完整回答
反对 回复 2019-06-12
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

可以选择多个文件,然后使用
<input type='file' name='file[]' multiple>
进行上传的示例php脚本:

<html><title>Upload</title><?php
    session_start();
    $target=$_POST['directory'];
        if($target[strlen($target)-1]!='/')
                $target=$target.'/';
            $count=0;
            foreach ($_FILES['file']['name'] as $filename) 
            {
                $temp=$target;
                $tmp=$_FILES['file']['tmp_name'][$count];
                $count=$count + 1;
                $temp=$temp.basename($filename);
                move_uploaded_file($tmp,$temp);
                $temp='';
                $tmp='';
            }
    header("location:../../views/upload.php");?></html>

将选定的文件作为数组接收

$_FILES['file']['name'][0]存储第一个文件的名称。
$_FILES['file']['name'][1]存储第二个文件的名称。
诸若此类。


查看完整回答
反对 回复 2019-06-12
  • 3 回答
  • 0 关注
  • 420 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信