PHP多文件上传仅首次上传图片

时间:2012-12-28 22:10:17

标签: php upload

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

<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 ++;

}

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

1 个答案:

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

相关问题