php 在 Laravel 5.2 的路径中找不到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37562850/
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-08-26 01:17:32  来源:igfitidea点击:

File not found at path in laravel 5.2

phplaravellaravel-5

提问by Zabs

I have an image within the public/badges directory called 1.png - I am trying to use the Storage library in laravel but keep getting the following error even though the file does exist at that location:

我在 public/badges 目录中有一个名为 1.png 的图像 - 我试图在 laravel 中使用 Storage 库,但即使该文件确实存在于该位置,也会不断收到以下错误:

// Error

// 错误

FileNotFoundException in Filesystem.php line 381:
File not found at path: Users/gk/Sites/mysite/public/badges/1.png

// Code

// 代码

$filePath = 'badges/1.png';
$path = public_path()."/".$filePath
Storage::disk('local')->get($path);
dd($contents);

回答by Pyton

Form Laravel 5.2 documentation:

形成 Laravel 5.2 文档:

When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory.

使用本地驱动时,注意所有文件操作都是相对于你的配置文件中定义的根目录。默认情况下,此值设置为 storage/app 目录。

So You are looking for file in: storage/app/Users/gk/Sites/mysite/public/badges/1.png

所以你正在寻找文件: storage/app/Users/gk/Sites/mysite/public/badges/1.png

Error is quite confusing.

错误相当混乱。

[Update]

[更新]

Add to config/filesystems.php

添加 config/filesystems.php

'disks' => [

    //... 

    'local_public' => [
        'driver' => 'local',
        'root'   => public_path(),
    ],

    //... 
],

then

然后

$filePath = 'badges/1.png';
$content = Storage::disk('local_public')->get($filePath);
dd($content);

回答by Miharbi Hernandez

I had the same problem in Laravel 5.7, this fix my problem:

我在 Laravel 5.7 中遇到了同样的问题,这解决了我的问题:

php artisan storage:link