Laravel 服务器,上传到 public_html 而不是上传到项目文件夹

Laravel server, upload to public_html rather than upload in project folder

我一直在尝试在这个网站上浏览寻找答案,但对我没有任何帮助,我想上传保存在 public_html 文件夹中的图像,而不是将其保存在项目文件夹中(我将其分开项目文件夹并将所有 public 文件放入 public_html 文件夹)。已经绑定 public.path 但它 returns 到我的项目文件夹,而不是 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 中像这样绑定了 public 路径。php

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

谢谢!

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(),
],

首先,将配置添加到配置文件夹中的 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';
});