微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找不到 Laravel 8 存储移动文件

如何解决找不到 Laravel 8 存储移动文件

我正在尝试将文件从临时路径移动到正确的路径。但是,无论我尝试什么,它总是会发回此文件未找到错误

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

我使用的是sail,因此路径中有“var/www/html/”,即使容器中的这条路径没有问题,因为可以从前端获取API到达这条路径,那就是临时文件的保存方式。

这是控制器中的代码

//...
$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();
        }

这一行突出显示错误回溯:

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

也试过用Storage::disk('public')->move(),或者public_storage,或者Storage::path(),或者只用纯字符串,都没有用。

通过运行 public/storage 将符号链接storage 设置为 PHP artisan storage:link

我在 laracast、stack overflow 或 github 上搜索了尽可能多的内容,但找不到解决方案。有没有人对这个问题有任何想法?

真的很感激。

解决方法

不要使用 storage_path 函数,因为方法 Storage::move( 有前缀路径 [app_path]/storage/app/

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。