NotWritableException 无法将图像数据写入 heroku 上的路径

NotWritableException Can't write image data to path on heroku

我正在尝试将图像数据保存到文件夹中。它在我的本地主机上工作正常但是当我将它移动到 heroku 时它给了我这个错误

NotWritableException
Can't write image data to path (https://arcane-peak- 
34431.herokuapp.com/public/images/categories/1527155055.jpg)

这是我的代码。

$image = $request->file('image');
    $imagename = $image->getClientOriginalExtension();
    $imagename = time().'.'.$imagename;

    $destinationPath = URL::to('public/images/restaurants');
    $img = Image::make($image->getRealPath());

    $img->resize(100, 100, function ($constraint) {
        $constraint->aspectRatio();
    })->save($destinationPath.'/'.$imagename);

    $destinationPath = URL::to('public/images/restaurants');

    $image->move($destinationPath, $imagename);

我做错了什么?而且在我的本地机器上,它是正在保存的上传图像,但我希望我调整大小的图像 saved.Any 帮助将是救命稻草

更大的问题是,即使您保存图像,它也会消失。 Heroku 使用 ephemeral filesystem(粗体添加):

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted. For example, this occurs any time a dyno is replaced due to application deployment and approximately once a day as part of normal dyno management.

Heroku recommends storing user uploads using a third-party service like Amazon S3.

你可以试试

Image::make($image)->save('images/restaurants/' . $imagename);

这对我有用

在我的例子中,git push heroku main 命令排除了图片目标的上传,因为它是空的。所以在这种情况下 public/images/categories 将不存在于 heroku 应用程序服务器中。

如果 public/images/categories 在线或不存在,您可以 运行 heroku run bash -a <APP_NAME> 检查应用程序文件夹。

为防止错误再次发生,您可以在 运行 git push 之前的文件夹中添加一个空的 index.php 文件,这将确保文件夹被上传。