将多个图像上载到文件夹,每个输入具有不同的名称

时间:2013-03-06 21:28:00

标签: php html

我正在尝试使用我管理的教程将多个图像上传到文件夹:

以PHP形式

<?php
  $success = 0;
  $fail = 0;
  $uploaddir = 'uploads/';
  for ($i=0;$i<4;$i++)
  {
   if($_FILES['userfile']['name'][$i])
   {
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
    $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
    if (preg_match("/(jpg|gif|png|bmp)/",$ext))
    {
     if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile)) 
     {
      $success++;
     } 
     else 
     {
     echo "Error Uploading the file. Retry after sometime.\n";
     $fail++;
     }
    }
    else
    {
     $fail++;
    }
   }
  }
  echo "<br> Number of files Uploaded:".$success;
  echo "<br> Number of files Failed:".$fail;
?>

在HTML表单中

<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
Image3: <input name="userfile[]" type="file" /><br />
Image4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>

正如您在HTML表单中看到的那样,输入名称是userfile []。现在我的HTML输入名称如下:picture01,picture02,picture 03等...

如何修改 PHP代码以使我的输入名称{:picture01,picture02,picture 03}而不是userfile []。

感谢。

更新

我希望上面的内容适合我的HTML表单

<form enctype="multipart/form-data" action="upload.php" method="post">
    Picture 01<input id="picture01"  name="picture01" type="file" ><br />
    Picture 02<input id="picture02"  name="picture02" type="file" ><br />
   Picture 03<input id="picture03"  name="picture03" type="file" ><br />
    Picture 04<input id="picture04"  name="picture04" type="file" ><br />
    <input type="submit" value="Upload" />
    </form>

2 个答案:

答案 0 :(得分:1)

此代码在本地运行。它使用了您的代码和php.net中的示例的组合。您应该使用pathinfo来获取扩展名,但这只是一个小细节。

form.html

<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
Image3: <input name="userfile[]" type="file" /><br />
Image4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>

upload.php的:

<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
$success = 0;
$fail = 0;

$uploads_dir = 'uploads';
$count = 1;
foreach ($_FILES["userfile"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
        $name = $_FILES["userfile"]["name"][$key];
        $uploadfile = "$uploads_dir/$name";
        $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
        if (preg_match("/(jpg|gif|png|bmp)/",$ext)){
            $newfile = "$uploads_dir/picture".str_pad($count++,2,'0',STR_PAD_LEFT).".".$ext;
            if(move_uploaded_file($tmp_name, $newfile)){
                $success++;
            }else{
                echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
                $fail++;
            }
        }else{
            echo "Invalid Extension.\n";
            $fail++;
        }
    }
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;

答案 1 :(得分:0)

如果更改了表单中的名称,请在尝试获取文件的数组元素时更改名称

离。

echo $_FILES["picture$i"]['name'];

并将此更改为

for ($i=1;$i<=4;$i++)