【问题标题】:Uploading multifiles上传多文件
【发布时间】:2015-02-01 00:27:56
【问题描述】:

我无法上传多个文件。我有一个包含 4 个项目的表格,每个项目都可能有一个上传的文件。

代码应该如何?

$target_dir = "uploads/";
    foreach ($_FILES as $f => $a) {
        if ($a["name"]) {
            $target_file = $target_dir . "paper_" . $id . "_" . $f[12];
            $file_name = basename($a["name"]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
             && $imageFileType != "pdf" && $imageFileType != "doc" && $imageFileType != "docx") {
                echo "Sorry, only JPG, JPEG, PNG, PDF, DOC and DOCX files are allowed. Your type is $imageFileType ";
                continue;
            }
            if ($a["size"] > 1024 * 1024 * 4) {
                echo "Sorry, your file is too large. Max is 4 MB";
                continue;
            }
            $col = "";
            switch ($f[12]) {
                case 1:
                    $col = "path_salary_def";
                    break;
                case 2:
                    $col = "path_social";
                    break;
                case 3:
                    $col = "path_identification";
                    break;
                case 4:
                    $col = "path_accom";
                    break;
                default:
                    die("invalid column");
            }
            $bol = substr($col, 5);
            if (move_uploaded_file($a["tmp_name"], $target_file)) {
                $query = "update papers SET $col = '$target_file', $bol = 1 WHERE id = " . $id;
                $result = mysqli_query($con,$query) or die ("Error in the data table 5");
                echo "The file $file_name has been uploaded.<br/>";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }

【问题讨论】:

  • 你遇到了什么错误?
  • 你糟糕的系统迫使我在我的问题结束时添加更多细节。我必须复制粘贴一些文本以便它接受它....多么糟糕的网站
  • here学习一些东西
  • @andrew 循环甚至不起作用。 $_FILES 为空
  • 系统是这样的,我们可以更好地帮助您

标签: php html


【解决方案1】:

如果您的$_FILES 数组为空,则可能您在表单中省略了enctype 属性。

应该是这样的:

<form action="yourAction.php" method="POST" enctype="multipart/form-data">

【讨论】:

  • 方法也必须是"POST/GET"吗??
  • 我找不到任何东西来确认我会坚持使用method ="POST"
  • 其实应该是POST。
  • 现在显示Warning: move_uploaded_file(uploads/paper_4_1): failed to open stream: No such file or directory in C:\xampp\htdocs\cms\admin\showroom_papers_save.php on line 79 Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php5E05.tmp' to 'uploads/paper_4_1' in C:\xampp\htdocs\cms\admin\showroom_papers_save.php on line 79 Sorry, there was an error uploading your file.
  • 您应该从文件数组中的['tmp_name'] 元素中移动文件,执行var_dump($_FILES) 您会明白我的意思,另请参阅here
猜你喜欢
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 2013-02-05
  • 2019-08-14
相关资源
最近更新 更多