多个图片上传PHP,循环?

时间:2012-12-18 14:47:09

标签: php mysql pdo

我试图将多张图片上传到我的服务器,如果上传单张图片,我有以下内容,但我不确定下一步该去哪儿?

我有一个提交3张图片的POST表单,以下只上传第一张图片,我是否需要为每张图片创建一个while循环?

到目前为止,我写过......

    $allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . date('U')."-".$_FILES["file"]["name"]);  // add a unique string to the uploaded filename so that it is unique.
      echo "Stored in: " . "upload/" . date('U')."-".$_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";

}

任何人都可以看到我出错的地方吗?

1 个答案:

答案 0 :(得分:0)

如果您想使用脚本,可以通过以下方式完成:

for($i = 0; $i < 3; $i++) {
 $aFile = $_FILES['file'][$i];

 // your code ($_FILES['file']['tmp_name'] should be addressed like $aFile['tmp_name'] and so on..
}