首页 > 解决方案 > 如何修复“无法将图像数据写入路径”Laravel 6.X

问题描述

我创建了一个媒体类来上传图像以存储在 Laravel 的存储公共路径中,但干预保存方法不起作用

我试图更改目录的权限,但没有。我还尝试对我的路径使用 public_path() 函数,或者对文件使用 getRealPath() 函数。我还检查了与此问题相关的许多其他类似问题,最后我认为这可能是 Laravel v.6 的问题

这是上传文件的功能:

/**
 * @param $entity
 * @param $file
 * @return string
 */
public function processFile($entity, $file)
{
    if (!$file->isValid()) {
        return false;
    }
    $imgName = preg_replace('@[a-z0-9]i@', '_', md5(uniqid(time())) . time());
    $fileName = $imgName . '.' . $file->extension();

    $pathSmall = "image/$entity/small/";
    $pathLarge = "image/$entity/large/";


    if (!Storage::exists($pathSmall)) {
        Storage::makeDirectory($pathSmall, 7777, true, true);
    }
    if (!Storage::exists($pathLarge)) {
        Storage::makeDirectory($pathLarge, 7777, true, true);
    }

    Image::make($file)->resize(310, 170)->save($pathSmall.$fileName);
    Image::make($file)->resize(1920, 1080)->save($pathLarge.$fileName);

    return $fileName;
}

我试图调试所有代码,问题是保存方法,图像制作和调整大小正确但保存不起作用:

Intervention\Image\Exception\NotWritableException: Can't write image data to path (image/post/small/c2839f74e99f1c7f7c798171cf8ecf7b1571167248.jpeg) in file /home/vagrant/code/api-blog/vendor/intervention/image/src/Intervention/Image/Image.php on line 150

标签: phplaravelintervention

解决方案


推荐阅读