【问题标题】:Laravel public URL for locally Uploaded FileLaravel 本地上传文件的公共 URL
【发布时间】:2017-10-11 01:58:04
【问题描述】:

我已将一个可公开访问的文件上传到我的本地磁盘。我可以在我的 IDE 中的 storage/app/public/mysubdir/myfile.js 中看到它。
我已经运行了 artisan 命令来创建指向公共目录的符号链接。我还可以在 public/storage/mysubdir/myfile.js 中看到该文件。
然而,当我转到http://my.app/storage/mysubdir/myfile.js 时,我收到错误“RouteCollection.php 第 179 行中的 NotFoundHttpException”。

有什么想法吗?我需要添加一些特殊的路线吗?还是更新 Nginx?

我关注了https://laravel.com/docs/5.4/requests#storing-uploaded-fileshttps://laravel.com/docs/5.4/filesystem#the-public-disk

干杯

【问题讨论】:

  • 你有到/storage的路径点吗?
  • 没有。据我所知没有。

标签: laravel laravel-5 laravel-5.4


【解决方案1】:

我发现了问题。我最初在本地机器上而不是在虚拟机上运行 php artisan storage:link。我删除了符号链接并在 Vagrant/Homestead 中重新运行 php artisan storage:link,然后它开始工作了。

【讨论】:

    【解决方案2】:

    请务必像这样公开存储文件:

    $request->file('file')->store('images', 'public');
    

    然后像这样恢复它:

    asset('storage/file.ext');
    

    了解更多:

    【讨论】:

    • 我也做过类似的事情。我正在做$file->storePubliclyAs(Auth::id(), $file->getClientOriginalName(), ['disk' => 'public']);asset(Storage::url($file))
    • 在此处写入保存在 DB 中的图像路径之一,并尝试 asset('storage/'.$model->image_path_saved_in_db)
    【解决方案3】:

    请检查我的代码上传图片以存储在本地和服务器端的公用文件夹中

    public function uploadImage(Request $request){
            $file =  Input::file('image'); 
            if(empty($file)){
                return Response()->json(['error' => "Image is required" ], 406);
            }
            $extension = $file->getClientOriginalExtension(); 
                $filename = rand(11111111111,999999999999).'.'.$extension; 
                $tmp_path = public_path('users/thumbnail/');
                $letmovefiles   = $file->move($tmp_path, $filename);
                $full = $tmp_path.$filename;
                $destinationPath = public_path('users/');
                if ($_FILES['image']) {
                    $image   = Image::make($full);
                    $image->resize(600, 600)->save();
                    $new_path = $destinationPath;
                  rename($destinationPath,$new_path);  
                }
              $getlnik = url('users/thumbnail').'/'.$filename;
             return Response()->json(['url' => $getlnik ], 200);
        } 
    

    【讨论】:

    • 看起来好像您将文件存储在公共目录中。如果部署为零停机时间,这将如何发挥作用?
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 2018-05-03
    • 2016-08-29
    • 2017-10-03
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多