gpt4 book ai didi

php - Laravel 应用存储图片加载失败并重定向到 404

转载 作者:行者123 更新时间:2023-12-02 06:13:48 25 4
gpt4 key购买 nike

我在共享托管服务器上托管 laravel 应用程序。
服务器上有2个文件夹:

  • 1) laravelapp -> 包含 (app,config,database,vendor...etc 文件夹)
  • 2) public_html -> 包含 (css,js,images,storage 文件夹)

  • 我使用下面的代码上传图片:
     if($request->hasFile('file')){
    $filenameWithExt=$request->file('file')->getClientOriginalName();
    $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
    $extension=$request->file('file')->getClientOriginalExtension();
    $fileNameToStore=str_slug($filename,'_').'_'.time().'.'.$extension;
    request()->file->move(public_path('storage/profile'), $fileNameToStore);
    $image="/storage/profile/".$fileNameToStore;
    }

    上传文件成功到 laravelapp/public/storage/profile文件夹

    我创建了指向 laravelapp/public/storage 的符号链接(symbolic link)与 public_html/storage
    在数据库中:图像路径保存为 /storage/profile/user1.png
    在我使用的 Blade 模板中: <img src="{{$img}}" />
    但是 imageurl :

    www.mydomain.com/storage/profile/user1.png redirect to 404 page and image not displaying .



    任何人请帮我解决问题。

    最佳答案

    这在共享托管服务器上对我有用:

    编辑您的 config/filesystems.php并修改public像这样的部分

    'public' => [
    'driver' => 'local',
    'root' => storage_path(),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
    ]

    请注意, storage_path() 不接受任何参数。现在删除 storage您的 public 中的文件夹目录如果已经存在,则执行:
    php artisan storage:link

    关于php - Laravel 应用存储图片加载失败并重定向到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49489549/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com