【问题标题】:creating image inside a folder results in creating an image with name folder/image.jpg在文件夹中创建图像会导致创建名称为 folder/image.jpg 的图像
【发布时间】:2016-08-28 23:41:18
【问题描述】:

我有这段代码:

    $file = $faker->image($dir = public_path().'/tmp', $width = 800, $height = 600, '', true);
    $hash = str_random(7);
    $thumbnailName = $hash . '.jpg';
    $thumbnailImage = ImgResizer::make($file)->fit(180, 180);
    $thumbnailImage->save( public_path() . '\\thumb\\'. $thumbnailName);
    rename($file, 'public/'.$hash.'.jpg');

如您所见,我正在使用faker 来填充数据库。 Faker 也支持图像,它从 lorempixel.com 获取图像。我将此图像直接保存在公用文件夹中,并且可以正常工作。我还使用InternventionImage 创建了图像的缩略图并将其保存到 public/thumb 文件夹,如您在代码中看到的那样。

当我运行 db:seed 时,没有产生任何错误,一切都很顺利。但是,当我查看拇指文件夹时,那里没有文件。在公用文件夹中创建了一堆图像,但在拇指中没有。我登录服务器并 cd 进入默认文件夹并输入 ls 并得到以下输出:

正如您在图片上看到的那样,那里有一堆public/thumb/hashcode.jpg 图像。这怎么可能,它们不在拇指文件夹中,为什么要在那里列出它们?脚本是否改为使用文件名public/thumb/hashcode.jpg 创建文件?如何将它们放在拇指文件夹中?此代码适用于我的本地机器 windows 10 下 apache2 和 php7。

【问题讨论】:

  • 问题在于以下代码 public_path() 。 '\\拇指\\'。 $thumbnailName 在 public_path() 中创建名称为 '\\thumb\\' 的图像。 $缩略图名称。使用 public_path() 。 '/拇指/'。 $thumbnailName 将它们保存在拇指中。

标签: php laravel ubuntu intervention


【解决方案1】:

尝试改变这个:

public_path() . '\\thumb\\'. $thumbnailName

到这里:

public_path().DIRECTORY_SEPARATOR.'thumb'.DIRECTORY_SEPARATOR.$thumbnailName

另外,创建thumb 目录并设置正确的权限:

chmod -R 755 /public/thumb

【讨论】:

  • 我结束了使用 public_path() 。 '/拇指/'。 $thumbnailName 上面 Andrej 的建议。但是你的答案也有效,所以我会接受它作为问题的答案。
猜你喜欢
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多