如何在一页上处理 3 个"input file"字段?



我不想要一个带有 multiple 属性的字段,我需要 3 个单独的输入,这些输入将存储在一个 MYSQL 行中。当我运行页面时,它只将第一个图像存储在数据库和文件系统中。我确信这与"tmp_name"有关,但我无法弄清楚如何解决它。

Array
(
    [name] => banner_large.jpg
    [type] => image/jpeg
    [tmp_name] => C:wamptmpphp3936.tmp
    [error] => 0
    [size] => 37536
)
Array
(
    [name] => banner_medium.jpg
    [type] => image/jpeg
    [tmp_name] => C:wamptmpphp3947.tmp
    [error] => 0
    [size] => 23017
)
Array
(
    [name] => banner_small.jpg
    [type] => image/jpeg
    [tmp_name] => C:wamptmpphp3948.tmp
    [error] => 0
    [size] => 13887
)

     $ct = 0;
     foreach ($_FILES as $value) {
        $filearray[$ct] = $value;
        $ct++;
     }
     foreach ($filearray as $file) {
        if ($file['error'] != 0) {
           // error: report what PHP says went wrong
           $this -> errors[] = $this -> upload_errors[$file['error']];
           return false;
        } else {
           $this -> temp_path = $file['tmp_name'];
           $this -> type = $file['type'];
           if ($loop == 0) {
              $this -> filename = basename($file['name']);
              $this -> size = $file['size'];
           } elseif ($loop == 1) {
              $this -> filenamem = basename($file['name']);
              $this -> sizem = $file['size'];
           } else {
              $this -> filenames = basename($file['name']);
              $this -> sizes = $file['size'];
           }
           return true;
        }
     }

请帮忙。

你要回到foreach loop里面。返回退出循环

 $ct = 0;
 foreach ($_FILES as $value) {
    $filearray[$ct] = $value;
    $ct++;
 }
 foreach ($filearray as $file) {
    if ($file['error'] != 0) {
       // error: report what PHP says went wrong
       $this -> errors[] = $this -> upload_errors[$file['error']];
       return false;
    } else {
       $this -> temp_path = $file['tmp_name'];
       $this -> type = $file['type'];
       if ($loop == 0) {
          $this -> filename = basename($file['name']);
          $this -> size = $file['size'];
       } elseif ($loop == 1) {
          $this -> filenamem = basename($file['name']);
          $this -> sizem = $file['size'];
       } else {
          $this -> filenames = basename($file['name']);
          $this -> sizes = $file['size'];
       }
    }
 }
 return true;

我必须提到的两件事。

  1. 为什么要遍历 $_FILES 数组并将其置于与它完全相同的状态?默认情况下,数组的编号从 0 开始,您只需再次执行该过程即可。因此,无需创建 fileArray,只需使用 $_FILES。

  2. 为什么要将图像属性设置为对象属性?这些属性不是数组,所以你可以做的是将它们变成数组(例如$this->temp_path[]),或者你可以在实际的foreach循环中插入到数据库中。

而且你的$loop也不是由它的外观来定义的。

最新更新