只使用PHP和mysql上传多个文件

时间:2012-08-31 04:19:59

标签: php arrays upload

我正在尝试创建一个动态多文件上传系统,其中用户指定要上载的文件数。选择完成后,将创建相应数量的文件上载字段,然后用户可以进行上载。

以下是我的脚本。从我的调试中成功创建了数组,但上传失败了。如果有人告诉我我做错了什么,我会非常感激。

<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>

1 个答案:

答案 0 :(得分:1)

如果您准备使用jquery,请尝试multiple uploads