PHP move_uploaded_file只上传一个文件

时间:2022-04-12 16:04:18

I'm testing uploads on 3 files on my desktop test.xlsx, test - Copy.xlsx, and test - Copy (2).xlsx. My PHP script is breaking at the line where my move_uploaded_files statement is. I get the error move_uploaded_file(uploads/test - Copy.xlsx): failed to open stream: No such file or directory. BUT test - Copy (2).xlsx is uploaded when I check my uploads folder. My PHP is as follows:

我正在测试我的桌面test.xlsx上的3个文件的上传,test - Copy.xlsx和test - Copy(2).xlsx。我的PHP脚本在我的move_uploaded_files语句所在的行中断开。我得到错误move_uploaded_file(uploads / test - Copy.xlsx):无法打开流:没有这样的文件或目录。但是测试 - 当我检查我的上传文件夹时,上传了复制(2).xlsx。我的PHP如下:

foreach($_FILES['file']['name'] as $key=>$value){
    $fileName = $_FILES['file']['name'][$key];
    $fileTmpLoc = $_FILES['file']['tmp_name'][$key];
    $fileErrorMsg = $_FILES['file']['error'][$key];
    $type = $_FILES['file']['type'][$key];
    if(){
        //validation
    } else{ //if validated
        if(move_uploaded_file($fileTmpLoc, 'uploads/'.$fileName)){
          //do more stuff
        } else{
            echo "Upload failed.";
        }
    }
}

The //do more stuff actually runs once for test - Copy (2).xlsx (the only file uploaded successfully). My JS script is below but I doubt that's the part that's breaking since when I print_r($_FILES) in PHP, all files appear in the output.

//更多的东西实际运行一次用于测试 - 复制(2).xlsx(唯一成功上传的文件)。我的JS脚本在下面,但我怀疑这是因为我在PHP中使用print_r($ _ FILES)时所有文件都出现在输出中。

var upload = function(event){
    var file = document.getElementById('file').files;
    var data = new FormData();
    for (i=0; i<file.length; i++){
        data.append('file[]', file[i]);
    }
    var request = new XMLHttpRequest();
    request.addEventListener('load', completeHandler, false);
    request.addEventListener('error', errorHandler, false);
    request.addEventListener('abort', abortHandler, false);
    request.open('POST', 'php/excel_upload_read.php');
    request.send(data);
};

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


1  

Looks like you are using the multi-dimensional array in the wrong order. Try this:

看起来您正在以错误的顺序使用多维数组。尝试这个:

foreach($_FILES['file'] as $key=>$value){
    $fileName = $_FILES['file'][$key]['name'];
    $fileTmpLoc = $_FILES['file'][$key]['tmp_name'];
    $fileErrorMsg = $_FILES['file'][$key]['error'];
    $type = $_FILES['file'][$key]['type'];
    ...
}

#2


1  

The problem is the way $_FILES global is structured, it does not let you iterate easily.

问题是$ _FILES全局结构的方式,它不会让你轻松迭代。

You can use this function it s meant to reorganize the data structure

您可以使用此功能来重新组织数据结构

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

Then you can use foreach easily:

然后你可以轻松使用foreach:

if(isset($_FILES['file']['tmp_name'], $_POST)) {
    $files = reArrayFiles($_FILES['file']);

    foreach ($files as $file) {
        $temp = $file['tmp_name'];
        $path = 'uploads/'.$file['name'];
        if(move_uploaded_file($temp, $path)){
            echo 'Upload succesful';
        }else{
            echo 'failed to upload';
        }
    }
}else{
    echo 'file not uploaded';
}

This function reArrayFiles was posted by someone in the PHP documentation: http://php.net/manual/en/features.file-upload.multiple.php

此函数reArrayFiles由PHP文档中的某人发布:http://php.net/manual/en/features.file-upload.multiple.php

#1


1  

Looks like you are using the multi-dimensional array in the wrong order. Try this:

看起来您正在以错误的顺序使用多维数组。尝试这个:

foreach($_FILES['file'] as $key=>$value){
    $fileName = $_FILES['file'][$key]['name'];
    $fileTmpLoc = $_FILES['file'][$key]['tmp_name'];
    $fileErrorMsg = $_FILES['file'][$key]['error'];
    $type = $_FILES['file'][$key]['type'];
    ...
}

#2


1  

The problem is the way $_FILES global is structured, it does not let you iterate easily.

问题是$ _FILES全局结构的方式,它不会让你轻松迭代。

You can use this function it s meant to reorganize the data structure

您可以使用此功能来重新组织数据结构

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

Then you can use foreach easily:

然后你可以轻松使用foreach:

if(isset($_FILES['file']['tmp_name'], $_POST)) {
    $files = reArrayFiles($_FILES['file']);

    foreach ($files as $file) {
        $temp = $file['tmp_name'];
        $path = 'uploads/'.$file['name'];
        if(move_uploaded_file($temp, $path)){
            echo 'Upload succesful';
        }else{
            echo 'failed to upload';
        }
    }
}else{
    echo 'file not uploaded';
}

This function reArrayFiles was posted by someone in the PHP documentation: http://php.net/manual/en/features.file-upload.multiple.php

此函数reArrayFiles由PHP文档中的某人发布:http://php.net/manual/en/features.file-upload.multiple.php