如何在一个页面上处理3个“输入文件”字段?

时间:2014-01-03 03:12:38

标签: php file-upload file-io

我不希望一个字段具有multiple属性,我需要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;
        }
     }

请帮忙。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

我要提两件事。

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

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

  3. 而且你的$循环也不是由它的外观定义的。