0

I use laravel 5.6

My method to upload image like this :

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;
}

The code works. If the method run, it will save a file in product folder

But I want to add a logic. If the image success save in product folder, then it will make a folder thumb in folder product

I try add this code below $file->move($destinationPath, $fileName); like this :

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

Then I rum my method, there exist error like this :

Parse error: syntax error, unexpected 'return' (T_RETURN)

It seems the code line is still wrong

How can I solve this problem?

moses toh
  • 12,344
  • 71
  • 243
  • 443

2 Answers2

2

you missed semicolan next to this line

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

put this

$file->move($destinationPathThumb, $fileName);
Hamelraj
  • 4,676
  • 4
  • 19
  • 42
  • Ok. Thanks. I want to ask. How do you think my code. Whether the process is correct like that? Or need to simplify code again? – moses toh Feb 23 '18 at 06:25
  • Images uploaded successfully in the product folder. But the image was not uploaded successfully in the thumbs folder. Seems the code : `$file->move($destinationPathThumb, $fileName);` is not working – moses toh Feb 23 '18 at 06:32
0

You are missing a semicolon.

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

Your IDE can show this you.

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
  • Ok. Thanks. I want to ask. How do you think my code. Whether the process is correct like that? Or need to simplify code again? – moses toh Feb 23 '18 at 06:25
  • Images uploaded successfully in the product folder. But the image was not uploaded successfully in the thumbs folder. Seems the code : `$file->move($destinationPathThumb, $fileName);` is not working – moses toh Feb 23 '18 at 06:32