【问题标题】:Laravel "Can't write image data to path ({$path})"Laravel“无法将图像数据写入路径({$path})”
【发布时间】:2020-05-07 18:39:44
【问题描述】:

我在 ubuntu 18.4 上创建 Laravel 项目。

我在/var/www/html/上传我的项目,我在home/sun/download/上传图片

运行此代码后:

$imageName = "thumb_{$size}.{$extension}";
$routePath = $url . '/products/' . $product->id . '/';

Image::make($file->path())->resize($size, null, function ($constraint) {
            $constraint->aspectRatio();
        })->save($routePath, $imageName);

显示此错误:

"Can't write image data to path (/home/sun/download/products/2/)"

如何发出这个问题?

【问题讨论】:

    标签: laravel file-permissions laravel-6 directory-permissions


    【解决方案1】:

    权限问题,您是否尝试过使用 Storage 门面?

    $Image->save(Storage::disk('your_disk')->path($routePath), $quality);
    

    这就是我在我的情况下存储图像的方式

    Image = Image::make($source);
    $Image->getCore()->stripImage();
    
    # Resize the image and save
    $Image->fit($width, $height, function ($constraint) {
         $constraint->upsize();
    });
    
    return $Image->save(Storage::disk('public')->path($destination), $quality);
    

    【讨论】:

    • 请多解释
    • 你是在你的项目之外写镜像,你需要检查运行 laravel 的用户是否有权限写入那个目录(os issue 不是 laravel 本身),使用 Laravel 提供的 Storage 门面会作为一种快速的解决方法,但它会将您的数据存储在您的 laravel 应用程序中 /var/www/html/storage/... 可以写入文件的位置,因为该存储文件夹具有 755/644 权限,所以如果您想存储文件,您需要查看什么在你的 Laravel 文件夹之外是 linux 文件夹权限
    • 我需要上传到home/sun/download/
    • 我建议了一些有用的新标签
    猜你喜欢
    • 2020-03-29
    • 2016-07-24
    • 2023-01-09
    • 1970-01-01
    • 2015-11-06
    • 2017-01-25
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    相关资源
    最近更新 更多