我无法将文件上传到 Laravel 5.4 中的 public_path 文件夹。我不明白出了什么问题,the documentation makes it look easy . $request
是表单的 POSTed 内容。 filename
是通过表单提交的文件。
public function uploadFile($request) {
if ($request->hasFile('filename') && $request->file('filename')->isValid()) {
$file = $request->filename;
$hash = uniqid(rand(10000,99999), true);
$directory = public_path('files/'.$hash);
if(File::makeDirectory($directory, 0775, true)) {
return $file->storeAs($directory, $file->getClientOriginalName());
}
}
return NULL;
}
目录已创建,但里面没有文件。如您所见,该文件夹具有 775 权限。
我试过添加尾部斜杠。我试过完全删除 public_path
。什么都不管用。
我做错了什么? :(
最佳答案
默认情况下,文件系统使用名为'local' 的默认磁盘,使用store、stroeAs 等将文件上传到存储/应用文件夹存储中...
文件系统配置文件位于 config/filesystems.php。
要么你可以改变'本地'下的根路径
从 'root' => storage_path('app'),
到 'root' => public_path('files'),
然后在您的代码中更改
$directory = public_path('files/'.$hash);
到 $directory = public_path($hash);
或者您可以在 config/filesystem.php 中创建新磁盘
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'my_upload' => [
'driver' => 'local',
'root' => public_path('files'),
'visibility' => 'public',
],
然后在存储文件时如下提到新磁盘
$file->storeAs($directory, $file->getClientOriginalName(), 'my_upload');
执行以上所有操作后,如果不起作用,请按顺序点击以下命令
php artisan config:clear
php artisan cache:clear
php artisan config:cache
关于php - Laravel - 无法使用 storeAs 将文件保存到 public_path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44833648/