将多个图像上传到所需的文件夹

时间:2014-04-28 11:45:55

标签: php database

在我的网站中,用户可以发布多张图片...... 所以我希望用户上传的所有图像都应该存储在名为“newsdata”的文件夹中,在这个文件夹中我需要名为该特定帖子的Id的文件夹,所有图像都应该存储在该文件夹中,图像应该命名为1 ,2,3,....

这是我试过的

    <div id="upload">
      <div id="drop">
      Drop Images Here
          or
     <a>Browse</a>
     <input type="file" name="upl[]" multiple />
     </div>
     <ul>
         <!-- The file uploads will be shown here -->
     </ul>
     </div>
   </div>

Php脚本:

$new_last_id = $_SESSION[$last_id];

mkdir ("../newsdata/" . $new_last_id, 0777);
$path="../newsdata/";
  $uploadpath = $path.$new_last_id."/";      //directory where the images are uploaded    now
 $i = '0';
    foreach ($_FILES['upl']['name'] as $filename) {
    ++$i;
    $uploadpath = $uploadpath .''.$i.'.jpg';
    if(move_uploaded_file($_FILES['upl']['name'], $uploadpath)) { 
    $data_2 = 'True';
    }   
if($data_2)
{
header("Location:../");
}

但没有收到预期的结果.... 帮帮我 .... 在此先感谢

1 个答案:

答案 0 :(得分:0)

如果您执行print_r($_FILES);,则可以看到输出如下所示:

Array
(
    [upl] => Array
        (
            [name] => Array
                (
                    [0] => Desert.jpg
                    [1] => Jellyfish.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpsmBI96
                    [1] => /tmp/phpT55Qye
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 845941
                    [1] => 775702
                )

        )

)

ie:$ _FILES ['upl'] ['name']是一个数组。

您执行了foreach ($_FILES['upl']['name'] as $filename) {...,但为什么您要再次访问foreach内的$_FILES['upl']['name']并将其传递给move_uploaded_file。您应该用$filename替换它,这是您当前的文件名。

foreach ($_FILES['upl']['name'] as $filename) {
    ++$i;
    $uploadpath = $uploadpath .''.$i.'.jpg';
    if(move_uploaded_file($filename, $uploadpath)) { 
        $data_2 = 'True';
    }   
    [...]
}