【问题标题】:PHP Multi file upload only first image uploadingPHP多文件上传只上传第一张图片
【发布时间】:2012-12-28 22:10:16
【问题描述】:

我正在使用以下文件输入的多个实例上传图像:

<input type="file" name="photos[]">

我已经像这样设置了表单属性:

<form action="?action=form" method="post" class="nice" enctype="multipart/form-data">

当我遍历文件数组时,我可以打印出多个文件名。

但一旦我尝试上传文件,它只会上传数组中的第一个文件。

这是我的 PHP:

$uploadDir = '/uploads/';

$getCurrentTimeStamp = date('m-d-Y_h.i.s', time());

// Set the allowed file extensions
$fileTypes = array('jpg', 'jpeg', 'png', 'gif'); // Allowed file extensions

$theIds = $_POST["id"];

function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 } 

 //This applies the function to our file  

 $fileCount = 1;
foreach ($_FILES["photos"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {


 $ext = findexts ($_FILES['photos']['name'][$key]) ; 

if (!empty($_FILES)) {
    $tempFile   = $_FILES['photos']['tmp_name'][$key];
    $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;

    $targetFile = $uploadDir . "equipment_photo_" .$theIds . "_".$fileCount."_". $getCurrentTimeStamp."." .$ext;
    $theFileNameToStore = "equipment_photo_" .$theIds . "_".$fileCount."_". $getCurrentTimeStamp."." .$ext;

    // Validate the filetype
    $fileParts = pathinfo($_FILES['photos']['name'][$key]);



    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

        // Save the file
        move_uploaded_file($tempFile,$targetFile);

        echo $theFileNameToStore;

    } else {

        // The file type wasn't allowed
        echo 'Invalid file type.';

    }
}


        }


        $fileCount ++;

}

任何想法为什么多个图像会回显但文件不会上传?

【问题讨论】:

  • 小注:如果你只是把$targetFile 放一行,而不是重复文件名,你可以直接做$targetFile = $uploadDir . $theFileNameToStore
  • 你不应该遍历 $_FILES['photos'],而不是 $_FILES['photos']['error'] 吗?
  • 我认为你需要一个循环计数并设置允许的最大文件数
  • 一旦$_FILES 数组存在,所有上传都已经完成。如果您可以遍历文件名,则说明您的处理有误。您可以发布var_dump( $_FILES ); 的输出并选择多个文件吗?
  • 这里是 var_dump( $_FILES ); array(1) { ["photos"]=&gt; array(5) { ["name"]=&gt; array(2) { [0]=&gt; string(14) "paper_swan.jpg" [1]=&gt; string(16) "wooden_walls.jpg" } ["type"]=&gt; array(2) { [0]=&gt; string(10) "image/jpeg" [1]=&gt; string(10) "image/jpeg" } ["tmp_name"]=&gt; array(2) { [0]=&gt; string(36) "/Applications/MAMP/tmp/php/phpyFPeIu" [1]=&gt; string(36) "/Applications/MAMP/tmp/php/phpRH6N2I" } ["error"]=&gt; array(2) { [0]=&gt; int(0) [1]=&gt; int(0) } ["size"]=&gt; array(2) { [0]=&gt; int(413005) [1]=&gt; int(1007252) } } }

标签: php upload


【解决方案1】:

为了让您的生活更轻松,我建议您使用此功能重新排序 $_FILES 全局

function rotate_array($source_array, $keep_keys = TRUE)
{
  $new_array = array();

  foreach ($source_array as $key => $value)
  {
     $value = ($keep_keys === TRUE) ? $value : array_values($value);
     foreach ($value as $k => $v)
     {
        $new_array[$k][$key] = $v;
     }
  }

  return $new_array;
}

然后在你的代码中使用它:

$fileCount = 1;
$files = rotate_array($_FILES['photos']);

foreach ($files as $file) {
  if (is_uploaded_file($file['tmp_name'])) {
    $ext = findexts($file['name']); 

    $tempFile   = $file['tmp_name'];
    $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;

    $theFileNameToStore = "equipment_photo_" .$theIds . "_".$fileCount."_". $getCurrentTimeStamp."." .$ext;
    $targetFile = $uploadDir . $theFileNameToStore;

    // Validate the filetype
    $fileParts = pathinfo($file['name']);

    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {
      // Save the file
      //move_uploaded_file($tempFile,$targetFile);
      // Sometimes move won't work for unknown reasons, try copying, this should work
      copy($tempFile, $targetFile);

      echo $theFileNameToStore;
    } else {
      // The file type wasn't allowed 
      echo 'Invalid file type.';
    }
  }
  $fileCount ++;
}

确保您的phpinfo() 显示max_file_uploadsupload_max_filesizepost_max_size 的正确值。在您的代码上或在您的 php.ini 文件中进行更改

【讨论】:

  • 这个定义可以清理一切。谢谢。但我仍然只能上传 1 张图片。
  • 即使使用 php.ini 配置?
  • 是的。我的 max_file_uploads 设置为 20,其他的都远高于我的文件需要。
  • 我忘记了我也遇到过这个问题,是因为我试图移动文件而不是复制,文件被移动后被删除,天知道为什么。所以我已经更新了代码,试试吧,它现在应该可以工作了
  • 我尝试用 Copy 代替,但它仍然只上传 1 个文件。
猜你喜欢
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2019-05-17
  • 1970-01-01
  • 2021-12-01
  • 2020-03-23
相关资源
最近更新 更多