dst8922 2019-08-02 14:52
浏览 447

如何获取要上载的文件数组

I am running into a scenario that I cannot get more than one file to upload. If I add more than one file, only the last one will send through. I know this means I need to put the files into an array, but when I try I get an error from the fileUpload class.

IF I change the input html's name to `name="uploadedFile[]"' then I get the following error in my fileUpload class:

PHP Warning: basename() expects parameter 1 to be string, array given in /php/fileUpload.php on line 11

PHP Warning: move_uploaded_file() expects parameter 1 to be string, array given in /php/fileUpload.php on line 46

Then in my Communication class, you will see that if there is more than one file being uploaded, I send it as a zip, so I'm not sure if anything would need to be changed in there.

Does anyone see what I need to do to get the array to work?

HTML:

<input type="file" name="uploadedFile" class="inputfile" id="uploadedFileTest" data-multiple-caption="{count} files selected" multiple>

FileUpload class:

class fileUpload
{

    public function __construct()
    {}
    public function upload() {

        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["uploadedFile"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            return 0;
// if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"], $target_file)) {
                return basename($_FILES["uploadedFile"]["name"]);
            } else {
                return 0;
            }
        }
    }
}

Rest of the php file:

$files = null;
if (!empty($_FILES['uploadedFile']['name']) && $_FILES['uploadedFile']['error'] != 4) {
    $fu = new fileUpload();
    $filename = $fu->upload();
    $template = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='https://mbkit.com/php/uploads/{$filename}'>{$filename}</a>", $template);
    clearstatcache();
}

Communication Class - what sends out the attachment(s):

if (!empty($file) && !$recipient && count($file['uploadedFile']['name']) > 1) {

                $f = new ZipArchive();
                $zip = $f->open('uploads/' . $file['uploadedFile']['name'][0] . ".zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
                if ($zip) {
                    for ($index = 0; $index < count($file['uploadedFile']['name']); $index++) {
//                        echo $file['uploadedFile']['name'][$index] . "
";
                        $f->addFile($file['uploadedFile']['tmp_name'][$index], $file['uploadedFile']['name'][$index]);
                    }
                    $f->close();

                    $message["attachment[0]"] = curl_file_create("uploads/{$file['uploadedFile']['name'][0]}.zip",
                        pathinfo("uploads/{$file['uploadedFile']['name'][0]}.zip", PATHINFO_EXTENSION),
                        $file['uploadedFile']['name'][0] . ".zip");
                } else {
                    throw new Exception("Could not zip the files.");
                }
//                $message['html'] = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='https://mbkit.com/php/uploads/{$file['uploadedFile']['name']}.zip'>{$file['uploadedFile']['name']}</a>", $message['html']);

            } elseif (count($file['uploadedFile']['name']) == 1) {
                $message["attachment[0]"] = curl_file_create("uploads/{$file['uploadedFile']['name']}",
                    pathinfo("uploads/{$file['uploadedFile']['name']}", PATHINFO_EXTENSION),
                    $file['uploadedFile']['name']);

            }
  • 写回答

1条回答 默认 最新

  • dongleman4760 2019-08-02 20:03
    关注

    Try inspecting the $_FILES array to see the structure of a single file upload and a mulit-file upload.

    Here is a small function you can use to visually inspect an array:

    function varDumpToString($var, $type=false){ 
        ob_start();
        var_export($var);
        $return = ob_get_clean();
    
        return ($type === 'web' 
            ? str_replace("
    ", '<br/>', $return)
            : $return
        );
    }
    

    Usage:

    echo varDumpToString($_FILES, 'web');
    

    Alternatively:

    error_log(varDumpToString($_FILES));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?