简体   繁体   English

move_uploaded_file在while循环中无法上传多个文件

[英]move_uploaded_file failing for multiple file upload in while loop

I am uploading multiple files using while loop, following is my code, move_uploaded_file always fails and I get message "error uploading File!". 我正在使用while循环上传多个文件,以下是我的代码,move_uploaded_file始终失败,并且出现消息“上传文件出错!”。 I am using the same code(code inside while loop without $counter) to upload single file in other use case, which works well, but when I use the same code in while loop where I think the code looks fine, but fails at move_uploaded_file ...please suggest me whats the problem and how can I resolve this? 我在其他用例中使用相同的代码(不带$ counter的while循环中的代码)上载单个文件,效果很好,但是当我在while循环中使用相同的代码时,我认为代码看起来不错,但在move_uploaded_file失败...请向我提出问题所在,如何解决?

$counter = 0;
while ($_FILES) {

if (!isset($_FILES['album_pics'])) {
    //required variables are empty
    die("File is empty!");
}
if ($_FILES['album_pics']['error'][$counter]) {
    //File upload error encountered
    die(upload_errors($_FILES['album_pics']['error'][$counter]));
}

$FileName = strtolower($_FILES['album_pics']['name'][$counter]); //uploaded file name
$ImageExt = substr($FileName, strrpos($FileName, '.')); //file extension
$FileType = $_FILES['album_pics']['type'][$counter]; //file type
$FileSize = $_FILES['album_pics']["size"][$counter]; //file size
$RandNumber = rand(0, 9999999999); //Random number to make each filename unique.
$uploaded_date = date("Y-m-d H:i:s");
$FileTitle = current(explode('.', $FileName));

switch (strtolower($FileType)) {
    case 'image/png':
    case 'image/gif':
    case 'image/jpeg':
    case 'application/pdf':
    case 'application/msword':
    case 'application/vnd.ms-excel':
    case 'application/x-zip-compressed':
    case 'text/plain':
    case 'text/html':
        break;
    default:
        die('Unsupported File!'); //output error
}

//File Title will be used as new File name
$NewFileName = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), strtolower(substr($FileTitle, 0, 10)));
$NewFileName = $NewFileName.'_'.$RandNumber.$ImageExt;
$UploadDirectory = 'ajax/'; 


if (move_uploaded_file($_FILES['album_pics']['tmp_name'][$counter], $UploadDirectory.$NewFileName)) {
    // here I will insert image metadata to database with the new image name

    $counter = (if result is true) ? $counter + 1 : $counter;// increase counter if insert returns true
} else {
    die('error uploading File!');
}

function upload_errors($err_code) {
    switch ($err_code) {
        case UPLOAD_ERR_INI_SIZE:
            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'.ini_get("upload_max_filesize");
        case UPLOAD_ERR_FORM_SIZE:
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
        case UPLOAD_ERR_PARTIAL:
            return 'The uploaded file was only partially uploaded';
        case UPLOAD_ERR_NO_FILE:
            return 'No file was uploaded';
        case UPLOAD_ERR_NO_TMP_DIR:
            return 'Missing a temporary folder';
        case UPLOAD_ERR_CANT_WRITE:
            return 'Failed to write file to disk';
        case UPLOAD_ERR_EXTENSION:
            return 'File upload stopped by extension';
        default:
            return 'Unknown upload error';
    }
} //function upload_erros() close   
} //while loop close

Rearrange your files initially to better loop over them. 首先重新排列文件,以更好地循环播放它们。

https://gist.github.com/lukeoliff/5531772 https://gist.github.com/lukeoliff/5531772

Might make the basis of this function easier to flesh out. 可能会使此功能的基础更容易充实。

<?php
  if (!empty($_FILES)) {
    $_FILES = rearrangeFiles($_FILES);
  }
?>

Turns the following... 打开以下...

Array
(
    [name] => Array
        (
            [0] => foo.txt
            [1] => bar.txt
        )

    [type] => Array
        (
            [0] => text/plain
            [1] => text/plain
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpYzdqkD
            [1] => /tmp/phpeEwEWG
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 123
            [1] => 456
        )
)

into.... 进入...

Array
(
    [0] => Array
        (
            [name] => foo.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpYzdqkD
            [error] => 0
            [size] => 123
        )

    [1] => Array
        (
            [name] => bar.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpeEwEWG
            [error] => 0
            [size] => 456
        )
)

请将该函数移至while循环之外。然后再次运行。

I have found the answer and I am posting this so that anybody who want the solution to multiple file upload can refer to this 我已经找到了答案,我正在发布此答案,以便任何需要多文件上传解决方案的人都可以参考此答案

Instead of using while loop I changed it to foreach loop this way 我没有使用while循环,而是将其更改为foreach循环

foreach($_FILES['album_pics']['error'] as $key => $error){
  all code remains the same as above in while loop, except every reference to $counter will be replaced by $key 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM