Laravel - 无法使用 storeAs 将文件保存到 public_path

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44833648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:14:15  来源:igfitidea点击:

Laravel - Can't save files to public_path using storeAs

phplaravellaravel-5.4

提问by Chuck Le Butt

I cannot upload files to the public_path folder in Laravel 5.4. I can't understand what's going wrong, the documentation makes it look easy. $requestis the POSTed contents of a form. filenameis a file submitted via the form.

我无法将文件上传到 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;
}

The directory is created, but there's no file inside. As you can see, the folder has 775 permissions.

目录已创建,但里面没有文件。如您所见,该文件夹具有 775 权限。

I've tried added a trailing slash. I've tried removing public_pathaltogether. Nothing works.

我试过添加一个尾部斜杠。我试过public_path完全删除。没有任何作用。

What am I doing wrong? :(

我究竟做错了什么?:(

回答by Ruchi

By default file system use your default disk named 'local'that upload files in storage/app folder store using store, stroeAsetc...

默认情况下,文件系统使用名为“本地”的默认磁盘,使用storestroeAs等上传存储/应用程序文件夹存储中的文件...

The filesystem configuration file is located at config/filesystems.php.

文件系统配置文件位于 config/filesystems.php。

either you can change root path under 'local'

您可以更改“本地”下的根路径

from 'root' => storage_path('app'),to 'root' => public_path('files'),

'root' => storage_path('app'),'root' => public_path('files'),

and then in your code change from

然后在你的代码中从

$directory = public_path('files/'.$hash);to $directory = public_path($hash);

$directory = public_path('files/'.$hash);$directory = public_path($hash);

OR you can create new disk in config/filesystem.php

或者您可以在 config/filesystem.php 中创建新磁盘

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'my_upload' => [
            'driver' => 'local',
            'root' => public_path('files'),
            'visibility' => 'public',
        ],

and then mention new disk as below while storing file

然后在存储文件时提到如下新磁盘

$file->storeAs($directory, $file->getClientOriginalName(), 'my_upload');

After performing all above if not work hit below commands in order

执行完以上所有操作后,按顺序点击以下命令

php artisan config:clear

php artisan cache:clear

php artisan config:cache

回答by AddWeb Solution Pvt Ltd

You can try this :

你可以试试这个:

if(File::makeDirectory($directory, 0775, true)) {
  return $file->store($directory, $file->getClientOriginalName());
}

Hope this help you !

希望这对你有帮助!