【问题标题】:How do I process 3 "input file" fields on one page?如何在一页上处理 3 个“输入文件”字段?
【发布时间】:2014-01-03 03:12:38
【问题描述】:

我不想要一个具有多个属性的字段,我需要 3 个单独的输入,它们将存储在一个 MYSQL 行中。当我运行页面时,它只将第一个图像存储在数据库和文件系统中。我确定这与“tmp_name”有关,但我不知道如何修复它。

Array
(
    [name] => banner_large.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\php3936.tmp
    [error] => 0
    [size] => 37536
)
Array
(
    [name] => banner_medium.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\php3947.tmp
    [error] => 0
    [size] => 23017
)
Array
(
    [name] => banner_small.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\php3948.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;
        }
     }

请帮忙。

【问题讨论】:

  • 你没有在任何地方定义$loop?此外,此代码不会将图像保存在任何地方。尝试上传完整代码
  • 谢谢,$loop 是之前创建的,但我没有增加它。感谢您的帮助,数据库现在存储了所有三个文件,但文件系统中仍然只存储一个文件。我会再次尝试追踪它。代码太大,无法全部上传。

标签: php file-upload file-io


【解决方案1】:

您将返回 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;

【讨论】:

  • 那个和 for 循环应该具有 $key=>$val 结构,如 foreach ($filearray as $loop=>$file) {
【解决方案2】:

我不得不提两件事。

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

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

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

【讨论】:

  • 我尝试访问 $_FILES[0] 等,但它给出了一个错误,所以我将对象从数组中剥离出来。 $loop 是之前定义的,但 if 没有增加(很尴尬)。现在一切正常。
猜你喜欢
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多