如果图像成功保存在laravel的文件夹中,如何制作文件夹拇指?

时间:2018-02-23 04:09:29

标签: php laravel file-upload directory laravel-5.6

我使用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)

然后我朗读我的方法,存在这样的错误:

  

解析错误:语法错误,意外“返回”(T_RETURN)

似乎代码行仍然是错误的

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

你错过了这一行旁边的半纤赛

$file->move($destinationPathThumb, $fileName)

把这个

$file->move($destinationPathThumb, $fileName);

答案 1 :(得分:0)

你错过了一个分号。

$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);

您的IDE可以向您展示。