2011-07-22 116 views
0

我知道這裏已經有不少話題了,而且我已經閱讀了其中的很多內容,但都無濟於事。我有一個表單用於上傳帶有說明的多個文件。如果用戶需要多個上傳/描述字段,他們可以點擊鏈接通過jquery添加另一個。該窗體的代碼是:php/jquery多文件上傳

<form action="adm.php?mode=upload" method="post"> 
    <input type="hidden" name="article_id" value="<?php echo $article_id; ?>" /> 
    <input type="hidden" name="new_path" value="<?php echo $new_path; ?>" /> 
    <div id="files"> 
     <div id="file" class="row"> 
      <input type="file" name="file[]" id="file" /><br /> 
      Description:<br /> 
      <textarea name="desc[]" cols="50" rows="5"></textarea> 
     </div> 
    </div> 
    <div> 
     <input type="submit" name="submit" value="Submit" /> 
    </div> 
</form> 
<a href="#" id="add_upload">Add File</a> 

點擊add_upload時

<div id="file" class="row"> 
    <input type="file" name="file[]" id="file" /><br /> 
    Description:<br /> 
    <textarea name="desc[]" cols="50" rows="5"></textarea> 
</div> 

的克隆被附加到文件DIV。 ?

我相信這是根據php.net的<input type="file" name="file[]" id="file" />正確使用,然而在adm.php模式聽衆腳本=上傳從來沒有在提交收到$ _FILES數組:

case 'upload' : 
    $path = request_var('new_path', ''); 
    $article_id = request_var('article_id', 0); 
    $error = array(); 
    $messages = array(); 

if(isset($_FILES['file']['tmp_name'])) 
{ 
    // Number of uploaded files 
    $num_files = count($_FILES['file']['tmp_name']); 

    //create the directory if it doesn't exist already 
    if(!is_dir($path)) 
    { 
     mkdir($path); 
    } 

    /** loop through the array of files ***/ 
    for($i=0; $i < $num_files;$i++) 
    { 
     // check if there is a file in the array 
     if(!is_uploaded_file($_FILES['file']['tmp_name'][$i])) 
     { 
      $messages[] = 'No file uploaded'; 
     } 
     else 
     { 
      // copy the file to the specified dir 
      if(@copy($_FILES['file']['tmp_name'][$i],$path.'/'.$_FILES['file']['name'][$i])) 
      { 
       $messages[] = $_FILES['file']['name'][$i].' uploaded'; 
      } 
      else 
      { 
       /*** an error message ***/ 
       $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed'; 
      } 
     } 
    } 
} 
else 
{ 
    $messages[] = 'No files set for upload??'; 
} 

$err_msg = $messages; 
include($root_path.'pages/header.php'); 
include($root_path.'pages/error.php'); 
include($root_path.'pages/column.php'); 
include($root_path.'pages/footer.php'); 
exit; 

休息;

我總是得到「沒有文件設置爲上傳?」錯誤,因爲文件沒有被傳輸。所有其他值都通過POST發送並且沒有問題地接收。我究竟做錯了什麼?

+0

var_dump你的$ _POST和$ _FILE變量,看看裏面有什麼。 – profitphp

+0

var_dump:'array(5){[「article_id」] => string(2)「24」[「new_path」] => string(30)「images/gallery/family_outings /」[「file」] = > array(2){[0] => string(12)「DSC01121.jpg」[1] => string(12)「DSC01126.jpg」} [「desc」] => array(2){[ => string(5)「test1」[1] => string(5)「test2」} [「submit」] => string(6)「Submit」} array {0} { – chaoskreator

+0

數組在最後是$ _FILES – chaoskreator

回答

1

您在HTML中的表單聲明需要設置enctype屬性。

<form enctype="multipart/form-data"> 

然後使用像firefox的livehttpheaders的東西,看看數據是否實際上通過。在添加enctype之後,PHP側的$ _FILES數組中應該有一些內容。

+0

轉儲的人,我知道這將是一件愚蠢的事情。謝謝! – chaoskreator