[英]Laravel uploaded file not appears in linked “public/storage” folder
When I run command: php artisan storage:link
, this creates folder /public/storage
.当我运行命令:
php artisan storage:link
时,这将创建文件夹/public/storage
。
then I have code, which handles uploaded file:然后我有代码,它处理上传的文件:
// get file original name and extension here, then generate file new name $fileNameToStore
// set file path
$path = $request->file('my_file')->storeAs('public/uploaded_imgs', $fileNameToStore);
Code works and uploaded file appears in /storage/app/public/uploaded_imgs/
folder, which is nice, though there is nothing in /public/storage
folder.代码有效并且上传的文件出现在
/storage/app/public/uploaded_imgs/
文件夹中,这很好,尽管/public/storage
文件夹中没有任何内容。
Why there is not uploaded_imgs
folder in /public/storage
directory?为什么
/public/storage
目录下没有uploaded_imgs
文件夹? What I'm doing wrong?我做错了什么?
In config/filesystems.php, you could do this... change the root element in public在 config/filesystems.php 中,您可以这样做...更改公共的根元素
Note: instead of upload you can use your folder name注意:您可以使用您的文件夹名称而不是上传
'disks' => [
'public' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
]
]
and you can access it by你可以通过
Storage::disk('public')->put('uploaded_imgs', $request->file('my_file'));
or或者
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
Then use it:然后使用它:
Storage::disk('uploads')->put('filename', $file_content);
Try this:尝试这个:
Storage::disk('public')->put('uploaded_imgs', $request->file('my_file'));
hopefully it will work.希望它会起作用。
When you run php artisan storage:link
command, Laravel generates a "storage" symlink under "public" folder which directs to /storage/app/public
so your code is correct.当您运行
php artisan storage:link
命令时,Laravel 在指向/storage/app/public
的“public”文件夹下生成一个“storage”符号链接,因此您的代码是正确的。
There is no public/storage
directory, its a symlink which directs to /storage/app/public
which is where your uploaded_imgs
folder has been generated.没有
public/storage
目录,它是一个指向/storage/app/public
的符号链接,这是您的uploaded_imgs
文件夹的生成位置。
public/storage => /storage/app/public
You can use the following code to upload a file:您可以使用以下代码上传文件:
$file = $request->file('my_file');
$fileNameWithoutExtension = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$path = "public/uploaded_imgs/" + $fileNameWithoutExtension +"."+$file->getClientOriginalExtension();
Storage::disk('local')->put($path, file_get_contents($file), 'public');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.