Laravel 8 Storage move file not found

4.3k views Asked by At

I am trying to move a file from a temporary path to the correct path. However no matter what I tried it always sends back this file not found error:

League\Flysystem\FileNotFoundException
File not found at path: var/www/html/storage/app/public/covers/tmp/608c07a082451-1619789728/81-536x354.jpeg

I am using sail thus there are 'var/www/html/' in the path, even though there is no problem with this path in the container since it is possible to reach this path from the frontend fetch API and that is how the temp file was saved.

Here is the code in the controller:

//...
$temp_file = TempFile::where('folder', $cover_folder)->first();
        if ($temp_file) {
            // retrieve file path and target path
            $filepath = storage_path("app/public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}");
            $target_path = storage_path("app/public/covers/{$course->id}/");

            Storage::move($filepath, $target_path);

            $course->cover_img = "{$target_path}/{$temp_file->filename}";
            $course->save();

            // remove temp directory and temp file record
            rmdir(storage_path("app/public/covers/tmp/{$temp_file->folder}/"));
            $temp_file->delete();
        }

The error traceback highlighted on this line:

 Storage::move($filepath, $target_path);

Also tried to use Storage::disk('public')->move(), or public_storage, or Storage::path(), or only pure string, all did not work.

Symlink is set from public/storage to storage by running php artisan storage:link.

I have searched as much as I could find on laracast, stack overflow, or github, but could not reach a solution. Does anyone have any idea on this issue?

Really really appreciate it.

1

There are 1 answers

1
yuanganteng On BEST ANSWER

dont use storage_path function because method Storage::move( have prefix path [app_path]/storage/app/

 Storage::move("public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}", "public/covers/{$course->id}/");