1

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.

convers39
  • 312
  • 5
  • 12
  • what do you get when you go to the path it's failing on? ```/www/html/storage/app/public/covers/tmp/608c07a082451-1619789728/81-536x354.jpeg``` – Ballard Apr 30 '21 at 14:06
  • 1
    Try with `$filepath = "public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}"; $target_path = "public/covers/{$course->id}/";` – porloscerros Ψ Apr 30 '21 at 15:09
  • Thank you for your help. Yes I did try with pure string only, finally it works. – convers39 May 01 '21 at 01:22

1 Answers1

5

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}/");
yuanganteng
  • 171
  • 9
  • I think you are right, I change to pure string and tried without 'public' since I set default disk as 'public' already, and finally it works.. Thank you! – convers39 May 01 '21 at 00:49