0

I am trying to upload an image in Laravel. Getting the following error:

"message": "Could not move the file \"C:\\xampp\\tmp\\php84AA.tmp\" to \"F:\\bvend\\bvend-web\\public\\uploads/products\\bvend-product-1666274539.jpg\" (move_uploaded_file(): Unable to move "C:\\xampp\\tmp\\php84AA.tmp" to "F:\\bvend\\bvend-web\\public\\uploads/products\\bvend-product-1666274539.jpg").",
    "exception": "Symfony\\Component\\HttpFoundation\\File\\Exception\\FileException",
    "file": "F:\\bvend\\bvend-web\\vendor\\symfony\\http-foundation\\File\\UploadedFile.php",
    "line": 177,
    "trace": [ .... ]

My code is given below:

public function uploadImage($image, $image_path)
{
    $path = config('global.' . $image_path . '_image_path');

    file_exists($image) && unlink($image);

    $image_name = 'bvend-' . $image_path . '-' . time() . '.' . $image->getClientOriginalExtension();

    $image->move($path, $image_name); // $path: public_path('uploads/products')
    return $image_name;
}

I understand its a simple issue but still no clue where it causing issue.

Ebi
  • 373
  • 2
  • 12
Wahidul Alam
  • 1,216
  • 4
  • 26
  • 56
  • 1
    In Laravel you should not upload to the poblic folder, but to the storage folder (`storage_path()`). and run `php artisan storage link` to create a symlink in public. – Gert B. Oct 20 '22 at 14:09
  • @GertB. thanks now the error is gone but the image file not moving to the destination folder. I checked the path which is correct. – Wahidul Alam Oct 20 '22 at 14:23
  • [upload file with laravel](https://stackoverflow.com/questions/71694681/upload-file-with-laravel) – steven7mwesigwa Oct 20 '22 at 14:35

2 Answers2

1

Edit

@WahidulAlam Please, try removing file_exists($image) && unlink($image);
– steven7mwesigwa - https://stackoverflow.com/posts/comments/130904221?noredirect=1

@WahidulAlam You're essentially deleting the temporary file/image before its copied or moved.
– steven7mwesigwa - https://stackoverflow.com/posts/comments/130904261?noredirect=1

ah this is the catch !! thanks a lot.
– Wahidul Alam - https://stackoverflow.com/posts/comments/130904399?noredirect=1


Specifying A File Name

If you do not want a filename to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the filename, and the (optional) disk as its arguments:

$path = $request->file('avatar')->storeAs(
    'avatars', $request->user()->id
);

You may also use the putFileAs method on the Storage facade, which will perform the same file storage operation as the example above:

$path = Storage::putFileAs(
    'avatars', $request->file('avatar'), $request->user()->id
);

Solution


public function uploadImage(\Illuminate\Http\UploadedFile $image, $image_path)
{
    return $image->storePubliclyAs(
    config('global.' . $image_path . '_image_path'),
    'bvend-' . $image_path . '-' . time(),
    ["disk" => "public"]
);
}

Addendum

Don't forget to create a symbolic link from public/storage to storage/app/public. I.e:
php artisan storage:link.

The Public Disk

Once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:

echo asset('storage/file.txt');

In Summary

$savedPath = $request->file("***REQUEST-INPUT-IMAGE-NAME-HERE***")->storePubliclyAs(
    "***IMAGE-PATH-HERE***",
    "***CUSTOM-FILENAME-HERE***",
    ["disk" => "public"]
);
steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
0

I am using this methodology in Laravel 9.

try this:

public function uploadImage($image, $image_path)
            {
               // $path = config('global.' . $image_path . '_image_path');

                file_exists($image) && unlink($image);

                $image_name = 'bvend-' . $image_path . '-' . time() . '.' . $image->getClientOriginalExtension();

                //$image->move($path, $image_name); // $path: public_path('uploads/products')
                $image->move(public_path('/images'), $image_name);
                return $image_name;
            }