【问题标题】:Why image not saved in thumb folder on laravel?为什么图像没有保存在 laravel 的拇指文件夹中?
【发布时间】:2018-08-04 03:10:12
【问题描述】:

我使用 laravel 5.6

我上传图片的方法是这样的:

public function uploadImage($file) {
    if($file) {
        $fileName = str_random(40) . '.' . $file->guessClientExtension();
    }
    $destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
    if(!File::exists($destinationPath)) {
        File::makeDirectory($destinationPath, 0755, true);
    }
    $file->move($destinationPath, $fileName);
    return $fileName;
}

代码有效。如果该方法运行,它将在产品文件夹中保存一个文件

但我想添加一个逻辑。如果图片成功保存在产品文件夹中,那么它将在文件夹产品中生成一个文件夹缩略图

我尝试在$file->move($destinationPath, $fileName); 下方添加此代码,如下所示:

$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
    File::makeDirectory($destinationPathThumb, 0755, true);
}
$file->move($destinationPathThumb, $fileName);

然后我运行我的方法,图片成功上传到产品文件夹中。但是图片没有成功上传到拇指文件夹中。似乎代码:$file->move($destinationPathThumb, $fileName); 不起作用

也许我的逻辑代码还是错的

我该如何解决这个问题?

【问题讨论】:

  • 我认为您需要复制图像,因为通过请求发送图像时,它只能移动到一个位置。

标签: php laravel image-uploading laravel-5.6


【解决方案1】:

您需要复制文件,因为单个图像的多次移动不会提供您想要的。

你可以这样做,

$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' .   DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
    File::makeDirectory($destinationPathThumb, 0755, true);
}

Storage::copy($destinationPath. DIRECTORY_SEPARATOR . $fileName , $destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);

希望你能理解。

【讨论】:

  • 存在这样的错误:File not found at path: home/vagrant/code/myapp/storage/app/public/product
  • @SuccessMan 我已经更新了我的答案,试试这个。较旧的只有目录的路径,但您需要文件的完整路径。
  • 谢谢。看看这个:stackoverflow.com/questions/48961920/…。也许你可以帮助我
  • 好的,我也看看。
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多