3

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

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

2 回答 2

1

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

表单.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>

上传.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;
于 2013-03-06T23:05:32.553 回答
0

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

前任。

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

并更改为这样

for ($i=1;$i<=4;$i++)
于 2013-03-06T21:36:14.280 回答