Chu*_*utt 5 php laravel laravel-5.4
我无法将文件上传到Laravel 5.4中的public_path文件夹.我无法理解出了什么问题,文档使它看起来很容易.$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;
}
Run Code Online (Sandbox Code Playgroud)
目录已创建,但内部没有文件.如您所见,该文件夹具有775权限.
我试过添加一个尾部斜杠.我试过public_path完全删除.什么都行不通.
我究竟做错了什么?:(
默认情况下,文件系统使用名为"local"的默认磁盘,该磁盘使用store,stroeAs等上传存储/ app文件夹存储中的文件...
文件系统配置文件位于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',
],
Run Code Online (Sandbox Code Playgroud)
然后在存储文件时提及下面的新磁盘
$file->storeAs($directory, $file->getClientOriginalName(), 'my_upload');
Run Code Online (Sandbox Code Playgroud)
执行以上所有操作后如果不能按顺序执行以下命令
php artisan config:clear
php artisan cache:clear
php artisan config:cache
Run Code Online (Sandbox Code Playgroud)
Add*_*Ltd -3
你可以试试这个:
if(File::makeDirectory($directory, 0775, true)) {
return $file->store($directory, $file->getClientOriginalName());
}
Run Code Online (Sandbox Code Playgroud)
希望这对您有帮助!