【问题标题】:How to save images in public_html instead of the default storage in laravel?如何将图像保存在 public_html 而不是 laravel 中的默认存储?
【发布时间】:2020-04-17 19:00:06
【问题描述】:

Laravel 共享主机保存图片问题:

情况:

  • 无法创建符号链接()"symlink() has been disabled for security reasons"
  • 不支持 ssh,只支持 ftp

如何将图像保存到public_html 让我们说一个文件storage/cover_image

这是开发过程中生成的快捷方式

if($request->hasFile('cover_image')){
            // Get filename with the extension
            $filenameWithExt = $request->file('cover_image')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('cover_image')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
       Storage::disk('my_upload')->put($fileNameToStore, $request->file('cover_image'));
            //$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }

我在my_upload 中制作了一个自定义磁盘,我不知道它保存文件的位置,但它没有保存在public_html/storage/cover_images

//custom my_disk code
'my_upload' => [
        'driver' => 'local',
        'root' => public_path().'/storage/cover_images',
        'visibility' => 'public',
    ],

如果我使用 storeAs 它总是存储不在public_html 中的storage,它也不是权限问题,因为我仍在测试我已将权限设置为777

【问题讨论】:

  • 只是一个简短的提示。不要像您使用的那样使用托管服务提供商,只需通过 digitalocean 之类的人租用 VPS,并完全控制
  • 现在我知道了
  • 您在使用共享主机吗?
  • 是的,它是一个共享主机

标签: php laravel laravel-5


【解决方案1】:

看起来你是在共享主机上,据我所知,大多数主机提供商都有他们对 php.ini 配置 cmiiw 的规则。

所以,如果你想把你的图片放在 public_html 上,你可以这样做:

$path = '/home/{your_shared_hosting_username}/public_html/cover_image';
$request->file('cover_image')->move($path, $filename);

【讨论】:

    【解决方案2】:

    经过数小时的研究和磨练最终解决了我的问题……帮助其他人。

    第 1 步:

    config/filesystem.php中创建自定义磁盘

    //In my case
    'my_upload' => [
            'driver' => 'local',
            'root' => public_path().'/storage',
            'visibility' => 'public',
        ],
    
    

    第 2 步:

    public_path() 更改为public_html

    //What worked for me 
    //Changed file: public_html/index.php 
    //after bootstrap declaration-> this is important
    
    $app->bind('path.public', function() {
        return __DIR__;
    });
    

    第 3 步:

    现在像这样保存你的文件

    // Handle File Upload
           if($request->hasFile('cover_image')){
               // Get filename with the extension
               $filenameWithExt = $request->file('cover_image')->getClientOriginalName();
               // Get just filename
               $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
               // Get just ext
               $extension = $request->file('cover_image')->getClientOriginalExtension();
               // Filename to store
               $fileNameToStore= $filename.'_'.time().'.'.$extension;
               // Upload Image
              $path = $request->file('cover_image')->storeAs('cover_images', $fileNameToStore,['disk' => 'my_upload']);
           if(!$path){
                   return false;
    
                   }
    
           } else {
               $fileNameToStore = 'noimage.jpg';
           }
    

    在我的情况下,我的图像将保存在public_html/storage/cover_images

    资源:

    How to upload files in Laravel directly into public folder?

    但这并不能解决问题,因为图像存储在 laravel public 中,因此您必须将默认的 laravel 公共更改为您现在的 public_html,这是您在共享主机中的公共。

    https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5

    在 laravel 5.8 上测试

    注意:我还注意到运行 laravel config:cache 会破坏保存位置..您应该避免使用此命令。

    如果你已经运行了命令config:clear undo's

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 2021-06-02
      • 2020-06-13
      • 2017-07-22
      • 1970-01-01
      • 2021-05-24
      • 2015-08-04
      • 1970-01-01
      • 2019-06-11
      相关资源
      最近更新 更多