【问题标题】:Laravel - Can't save files to public_path using storeAsLaravel - 无法使用 storeAs 将文件保存到 public_path
【发布时间】:2017-06-29 19:39:24
【问题描述】:

我无法将文件上传到 Laravel 5.4 中的 public_path 文件夹。我不明白出了什么问题,@ 987654321@。 $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。没有任何效果。

我做错了什么? :(

【问题讨论】:

  • 名称为605915955555e4973a6.15945923的目录是否存在?
  • @SR_ 我已经读过在基于 Linux 的系统上不需要这样做,但如果不需要,我会添加一些东西来创建它,看看是否有帮助。谢谢。
  • 它有效???????
  • @SR_ 谢谢!创建目录修复了缩略图的创建,但奇怪的是原始文件(通过saveAs)没有存储。我现在可以将我的问题缩小到一个问题。非常感谢。
  • 更新了答案

标签: php laravel laravel-5.4


【解决方案1】:

默认文件系统使用名为 'local' 的默认磁盘,该磁盘使用 storestroeAs 等在 storage/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',
        ],

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

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

在执行完以上所有操作后,如果不工作,请按顺序点击以下命令

php artisan config:clear

php artisan cache:clear

php artisan config:cache

【讨论】:

    【解决方案2】:

    你可以试试这个:

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

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多