首页 > 解决方案 > 使用 laravel 将文件夹上传到服务器

问题描述

我正在尝试编写一个允许用户上传文件夹的脚本。基本上用户会指定一个文件夹,所有包含的文件都应该上传,在服务器上重建相对路径或文件夹结构。

html 部分使用webkitdirectory,

<form action="cmd/test" enctype="multipart/form-data" method="post">
    <input id="upload" name="content[]" type="file" value="Input" directory webkitdirectory>
    <input type="submit" value="submit">
</form>

服务器是用 Laravel 编写的。我希望Request在 Laravel 中使用,比如

Route::any('cmd/test', function(\Illuminate\Http\Request $request) {
    echo $request;
    $files = $request->file('content');
    echo json_encode($files);
    //Store the files here...
});

我尝试上传一个测试文件夹,内容如下:

dir /s test

 Directory of C:\Users\███████\Desktop\test

18-Nov-18  05:19 PM    <DIR>          .
18-Nov-18  05:19 PM    <DIR>          ..
18-Nov-18  05:19 PM                 1 note1.txt
18-Nov-18  05:19 PM                 1 note2.txt
18-Nov-18  05:20 PM    <DIR>          test1
               2 File(s)              2 bytes

 Directory of C:\Users\███████\Desktop\test\test1

18-Nov-18  05:20 PM    <DIR>          .
18-Nov-18  05:20 PM    <DIR>          ..
18-Nov-18  05:20 PM                 1 note3.txt
               1 File(s)              1 bytes

     Total Files Listed:
               3 File(s)              3 bytes

令我惊讶的是,测试脚本的输出是

[{},{},{}]

使用 不会检索有关文件名或内容的信息Request::file()。Edge 或 Firefox 的结果相同。有什么建议么?


编辑:基本上我没有意识到返回值的类型是UploadedFile. 获取文件的相对路径以重建文件夹结构的问题仍然存在。

标签: phplaravellaravel-5.7

解决方案


推荐阅读