【问题标题】:Multiple file upload in different fields不同领域的多个文件上传
【发布时间】:2014-07-31 14:38:09
【问题描述】:

我在从表单上传大量文件时遇到问题。我需要单独制作输入字段,我的表单是这样的:

<form action="upload.php" method="post" id="form" name="form" enctype="multipart/form-data">

  <input type="file" name="upload[]" >
  <input type="file" name="upload[]" >
  ...(more inputs)
  <input type="file" name="upload[]" >

  <button id="submit-button">Upload</button>

</form>

我在这个项目中使用了 jQuery 1.9,除了这个上传之外,我似乎找不到任何适合我想要做的事情。我发现了很多多输入的东西,但这样我就无法区分每个文件,我需要将每个文件的 url 保存到我的数据库中的右列。

我使用了一些我在其他类似问题上找到的代码,但它们似乎不起作用。我已经试过这个了:

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ((array)$_FILES['upload']['name'] as $f => $name) {     
    if ($_FILES['upload']['error'][$f] == 4) {
        continue; // Skip file if any error found
    }          
    if ($_FILES['upload']['error'][$f] == 0) {             
        if ($_FILES['upload']['size'][$f] > $max_file_size) {
            $message[] = "$name is too large!.";
            continue; // Skip large files
        }
        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
            $message[] = "$name is not a valid format";
            continue; // Skip invalid file formats
        }
        else{ // No error found! Move uploaded files 
            if(move_uploaded_file($_FILES["upload"]["tmp_name"][$f], $path.$name))
            $count++; // Number of successfully uploaded file
        }
    }
}
}

我什么都没有,我尝试上传的文件没有显示在服务器上。我已经检查了 php_info() 并且似乎启用了上传,并且由于我正在上传一个 .pdf,上面只写了大约 7kb 的“测试”,所以我认为这里的大小不是问题。

希望大家能帮帮我,谢谢。

更新

我已删除(数组)强制转换,但出现以下错误:

警告:在 path_of_file 中为 foreach() 提供的参数无效

【问题讨论】:

  • 你为什么要打字? (array)$_FILES['upload']['name'] 它已经是一个数组了
  • 是否启用了错误?你得到什么错误?检查您的日志或确保 display_errors 已打开。
  • 原始代码没有它,我看到一些 cmet 报告了与我的问题类似的东西,我想我可以试试看是否是问题所在。
  • 如果你的 if 子句不成立,则输出。
  • 原来它通过了第一个“if”然后它似乎忽略了 foreach 并直接到最后,什么都不上传。

标签: php html upload


【解决方案1】:

您在其中缺少非常重要的元素,即enctype

enctype='multipart/form-data'

在您的表单标签中使用它并再次检查。

<form action="upload.php" method="post" enctype='multipart/form-data' id="form" name="form">

->对于您的错误,使用以下代码(更新)上传多张图片

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files

foreach ($_FILES['username']['name'] as $f => $name) {

    $path = 'uploads'; //path of directory

    if ($_FILES['username']['error'][$f] == 4) {
        continue; // Skip file if any error found
    } else {
        if ($_FILES['username']['size'][$f] > $max_file_size) {
            $message[] = "$name is too large!.";
            continue; // Skip large files
        }
        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
            $message[] = "$name is not a valid format";
            continue; // Skip invalid file formats
        }
        else {
            // No error found! Move uploaded files 
            //$name_of_file = $_FILES['username']['name'][$f];
            $temp_name = $_FILES['username']['tmp_name'][$f]; //[$count];
            move_uploaded_file($temp_name, "$path/"."$name");
            $count++; // Number of successfully uploaded file
        }
    }
}
}

【讨论】:

  • 很抱歉,我仍然遇到同样的错误。但无论如何,谢谢,我现在已经添加了元素!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多