2014-12-03 93 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