【问题标题】:Laravel server, upload to public_html rather than upload in project folderLaravel 服务器,上传到 public_html 而不是上传到项目文件夹
【发布时间】:2020-09-03 12:09:25
【问题描述】:

我一直在尝试在此站点上浏览以寻找答案,但对我没有任何作用,我想上传保存在 public_html 文件夹中的图像,而不是将其保存在项目文件夹中(我将项目文件夹分开并将所有public_html 文件夹中的公共文件)。已经绑定 public.path 但它返回到我的项目文件夹,而不是 public_html 一个。

我的图片上传控制器

public static function uploadSubmit($request, $type, $for)
{
    if($request->hasFile('photos'))
    {
        $allowedfileExtension=['jpg','png'];
        $files = $request->file('photos');
        foreach($files as $file)
        {
            $extension = $file->getClientOriginalExtension();

            $check=in_array($extension,$allowedfileExtension);
            //dd($check);
            if($check)
            {       
                    $idx = Image::create([
                    'type' => $type,
                    'ids' => $for,
                    'ext' => $extension,
                    ])->id;
                    $filename = $idx . '.' . $file->getClientOriginalExtension();
                    $filename = $file->storeAs(public_path('images/'), $filename);                
            }
        }
    }
}

我已经在 public_html/index.php 中进行了这样的公共路径绑定

$app->bind('path.public', function() {
return __DIR__;
});

谢谢!

【问题讨论】:

    标签: laravel server upload


    【解决方案1】:

    首先,将配置添加到 config 文件夹中的 filesystem.php:

    'disks' => [
    
            ...
    
            'public_html' => [
                'driver' => 'local',
                'root' => public_path(),
                'visibility' => 'public',
            ],
    
            ...
    
        ],
    

    二、用代码存储图片:

    $file->storeAs('images', $filename, 'public_html')
    

    P/S:记得配置public_html目录的正确路径:

    $app->bind('path.public', function(){
        return __DIR__.'/../../public_html';
    });
    

    【讨论】:

    • 图像仍未上传到 public_html...我的代码实际发生了什么...
    • public_html的路径配置正确了吗?
    • 放到bootstrap/app.php中
    【解决方案2】:

    public 磁盘用于存储可公开访问的文件。默认情况下,public 磁盘使用local 驱动程序并将这些文件存储在storage/app/public 中。

    使用local 驱动程序时,所有文件操作都与filesystems 配置文件中定义的root 目录相关。默认情况下,此值设置为storage/app 目录。因此,以下方法会将文件存储在storage/app/file.txt

    Storage::disk('local')->put('file.txt', 'Contents');
    

    或者在你的情况下storage/app/images/file.txt

    $file->storeAs(public_path('images/'), $filename);
    

    您可以通过更改filesystems 配置文件来更改这些文件的存储位置。

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

    【讨论】:

    • 它返回 laravel 项目文件夹 public 文件夹,而不是 public_html 文件夹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2011-10-08
    • 2015-02-27
    • 2022-11-20
    • 1970-01-01
    • 2011-10-28
    相关资源
    最近更新 更多