2014-12-03 133 views
-2

我无法上传多个文件。我有一个包含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

,你得到什么错误? – andrew 2014-12-03 12:30:35

+0

当我的问题结束时,您可怜的系统迫使我添加更多细节。我必须复制粘贴一些文本,以便它接受它....多么糟糕的网站 – 2014-12-03 12:31:02

+0

从[这里]了解一些东西(http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php ) – 2014-12-03 12:31:16

回答

1

如果您$_FILES数组为空也许你省略了enctype属性构成形式。

它应该看起来像:

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

该方法必须是“POST/GET”吗? – 2014-12-03 12:45:51

+0

我找不到任何东西来确认,我会坚持'method =“POST”' – andrew 2014-12-03 12:47:09

+0

其实,它一定是POST。 – 2014-12-03 12:49:34