Laravel-无法使用storeAs将文件保存到public_path [英] Laravel - Can't save files to public_path using storeAs

查看:1372
本文介绍了Laravel-无法使用storeAs将文件保存到public_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将文件上传到Laravel 5.4中的public_path文件夹.我不明白发生了什么问题,该文档使查找变得容易. $request是表单的过帐内容. filename是通过表单提交的文件.

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. $request is the POSTed contents of a form. filename is a file submitted via the form.

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权限.

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

我尝试添加一个斜杠.我试着完全删除public_path.什么都行不通.

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

我做错了什么? :(

推荐答案

默认文件系统使用名为'local'的默认磁盘,该默认磁盘使用 store上传文件到存储/应用文件夹存储中 stroeAs 等...

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

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

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

您可以在本地"下更改根路径

either you can change root path under 'local'

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

然后在您的代码中更改

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

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

OR you can create new disk in 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

这篇关于Laravel-无法使用storeAs将文件保存到public_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆