我无法上传多个文件。我有一个包含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.";
}
}
}
答案 0 :(得分:1)
如果您的$_FILES
数组为空,则可能从表单中省略了enctype
属性。
应该看起来像:
<form action="yourAction.php" method="POST" enctype="multipart/form-data">