繁体   English   中英

在 Laravel 中上传照片

[英]Upload photo in Laravel

Laravel 文件上传在尝试上传图像时给用户错误

Get error: local.ERROR: Driver [] is not supported.

如何解决它的问题?

public function channelAvatar(Request $request, Channel $channel)
{
    // validate
    $this->validate($request, [
        'photo' => ['required', 'image', Rule::dimensions()->minWidth(250)->minHeight(250)->ratio(1 / 1)],
    ]);

    // fill variables
    $filename = time() . str_random(16) . '.png';
    $image = Image::make($request->file('photo')->getRealPath());
    $folder = 'channels/avatars';

    // crop it
    $image = $image->resize(250, 250);

    // optimize it
    $image->encode('png', 60);

    // upload it
    Storage::put($folder.'/'.$filename, $image->__toString());
    $imageAddress = $this->webAddress() . $folder . '/' . $filename;

    // delete the old avatar
    Storage::delete('channels/avatars/' . str_after($channel->avatar, 'channels/avatars/'));

    // update channel's avatar
    $channel->update([
        'avatar' => $imageAddress,
    ]);
    $this->putChannelInTheCache($channel);

    return $imageAddress;
}

在本地上传或上传到FTP仍然会出现相同的错误。

每当您在没有特定磁盘的情况下使用 Storage 选项时,Laravel 都会使用默认驱动程序。 似乎您已将local指定为默认驱动程序,但您没有配置它。

根据你的config/filesystem.php你有:

'ftp' => [ 
    'driver' => 'ftp', 
    'host' => env('FTP_HOST', 'test.something.net'), 
    'username' => env('FTP_USERNAME', 'someusername'), 
    'password' => env('FTP_PASSWORD', '*******'), 
],

因此您需要将其指定为默认驱动程序。 您可以通过添加:

FILESYSTEM_DRIVER=ftp.env文件中。

然后在 'config/filesystem.php` 中添加以下内容:

'default' => env('FILESYSTEM_DRIVER', 'local'),

现在,每当您执行Storage::something() ,它将使用默认驱动程序。 (您将local作为默认的东西`

如果您愿意,也可以指定它:

Storage::disk('ftp')->something()但是如果您的所有存储操作都使用一个磁盘,那么最好指定为默认值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM