0

我已成功上传一张图片,但无法处理多张图片。如何上传第二张图片?我是否在数据库中创建一个新字段并将其命名为 photo02,然后输入另一个名为 photo2 的表单输入。如何更改目标以上传和移动两个文件?

表格

Photo: <input type="file" name="photo" multiple>
Photo: <input type="file" name="photo" multiple>

编码

$target = "upload/"; 
$target = $target . basename( $_FILES['photo']['name']);

$pic=($_FILES['photo']['name']); 

mysql_query("INSERT INTO `employees` VALUES ('$pic' )") ; 

if(move_uploaded_file($_FILES ['photo'] ['tmp_name'], $target)) 
{ 
require_once 'SimpleImage.php';
$image = new SimpleImage();
$image->load($target);
$image->resize(50,50);
$image->save($target); 

输出

echo '<b> Picture 1: </b>','<img src="/upload/' . $row->photo . '" border=1>';
echo '<b> Picture 2: </b>','<img src="/upload/' . $row->photo . '" border=1>';
4

2 回答 2

0

在数组 $_FILES 上使用 foreach

if(isset($_FILES ['uploaded_files']))
{
     **foreach($_FILES['uploaded_files']['name'] as $key=>$value)**
     {
          if(is_uploaded_file($_FILES['uploaded_files']['tmp_name'][$key]) && $_FILES[['uploaded_files']['error'][$key] == 0)
          {
                $filename = $_FILES['uploaded_files']['name'][$key];
                $filename = time().rand(0,999).$filename;

                if(move_uploaded_file($_FILES['uploaded_files']['tmp_name'][$key], 'uploads/'. $filename))
                {
                      echo 'The file '. $_FILES['uploaded_files']['name'][$key].' was uploaded successful';
                }
                else
                {
                      echo 'There was a problem uploading the picture.';
                } 
          }
          else
          {
            echo 'There is a problem with the uploading system.';
          }
     }
}

?>
于 2013-02-21T21:41:55.557 回答
0

练习是一样的,你只需要放一个多重属性:

 <input type="file" name="photo" multiple>

然后 $_FILES 数组将包含更多图片。

您可以使用 print_r($_FILES); 查看它

于 2013-02-21T17:03:06.993 回答