2012-08-31 17 views
0

我想創建一個動態的多文件上傳系統,用戶指定要上傳的文件數量。選擇完成後,將創建相應數量的文件上傳字段,然後用戶可以進行上載。多個文件上傳只有PHP和mysql

下面是我的腳本。從我的調試中,數組已成功創建,但上傳失敗。如果有人告訴我我做錯了什麼,我將非常感激。

<form action="" method="post" enctype="application/x-www-form-urlencoded" name="max"> 
    <table width="100%" border="0" cellspacing="2" cellpadding="2"> 
    <tr> 
     <td >no of products</td> 
     <td><input name="max2" type="text" id="max2" size="3" maxlength="2" /> 
     </td> 
     <td><input type="submit" name="go" id="go" value="go&gt;&gt;" /></td> 
    </tr> 
    </table> 
</form> 
<form action="" method="post" enctype="multipart/form-data" name="uploader" id="uploader"><table> 
    <tr> 
<td> 

    <?php 
global $max; 
if(isset($_POST['max2'])) 
    $max = $_POST['max2']; 
else 
    $max = 3; 

for ($i = 1; $i <= $max; $i++) { 
$forms = '<table> 
      <tr> 
      <td>File</td> 
      <td><label for="uploader"></label> 
       <input name="uploader'.$i.'" type="file" id="uploader'.$i.'" /></td> 
      </tr> 
     </table>'; 
echo $forms.'<br>';//// creates a new form field depending on number of files specified by user 
} 
?> 
<input name="max3" type="hidden" id="max" value="<?php echo $_POST['max2'] ?>" /> 
<?php 
    if(isset($_POST['upload'])){ 

$uploadArray= array(); 
for ($i = 1;$i <= $_POST['max3']; $i++) { 
$uploadArray[] = $_FILES['uploader'.$i]['name']; 
     } 
print_r ($uploadArray); // display array to check it was properly created 


foreach($uploadArray as $file) { 

    $target_path = "../Users/storename/upload/"; 

     if(file_exists($target_path) && is_dir($target_path)){ 

       if(move_uploaded_file($_FILES[$file]["tmp_name"], $target_path.'new'.$file)) { 
        echo "<br> The file ". basename($_FILES['$file']['name'])." has been uploaded"; 
       } 
       else{ 
        echo "<br>The file ".$file." has NOT been uploaded"; 
       } 

    } 
    else{ 
    echo 'invalid path<br>'; 
    echo $target_path; 
    } 
} 
} 

?></td> 
    </tr> 
    <tr> 
    <td><input type="submit" name="upload" id="upload" value="Submit" /></td> 
    </tr> 
    </table> 

</form> 
+0

你不想使用jquery。 – ravi404

回答