【问题标题】:Laravel - How to move file from local storage to another storage?Laravel - 如何将文件从本地存储移动到另一个存储?
【发布时间】:2020-07-26 07:55:00
【问题描述】:

我的目标是将一个大文件上传到保管箱,因为等待时间可能太长,我想拆分进程并通过队列上传文件。

我正在做以下事情:

  • 我上传了一个文件(可能很大)

  • 我将其保存在本地存储中

  • 我将有关文件的数据保存在数据库中。
  • 我想在队列中获取文件并将其移动到保管箱磁盘。

问题是当我执行最后一步时出现以下错误

ErrorException
fopen(files/7u7v6LYq72vmXLqeWPsc6b0khiy9pEbFicVJuK2W.pdf): failed to open stream: No such file or directory

我尝试了不同的方法,但找不到解决方案。

我的代码

控制器方法:

public function uploadToDropbox(Request $request){
        $data = $request->validate([
            'file' => 'required|mimes:jpeg,jpg,png,doc,docx,pdf,txt,mp3,mp4,avi|max:600000',
            'first_name' => 'required',
            'last_name' => 'required',
        ]);

        /** @var \Symfony\Component\HttpFoundation\File\File $uploadedFile */
        $uploadedFile = $data['file'];

        $path = Storage::disk('local')->putFileAs( 'file', $uploadedFile, $uploadedFile->getClientOriginalName());

        $file = new File();
        $file->first_name = $data['first_name'];
        $file->last_name = $data['last_name'];
        $file->file = $path;
        $file->original_name = $uploadedFile->getClientOriginalName();
        $file->size = $uploadedFile->getSize();
        $file->real_path = $uploadedFile->getRealPath();
        $file->save();

        $result = ProcessFile::dispatch($file);

        if($result){
            return Redirect::back()->withErrors(['msg'=>'Successfully file uploaded']);
        } else {
            return Redirect::back()->withErrors(['msg'=>'File failed to upload']);
        }
}

队列作业:

public function handle()
{
        if (Storage::disk('local')->exists($this->file->file)) {
            $name = strtolower($this->file->first_name) . '_' . strtolower($this->file->last_name);

            $rez = Storage::disk('dropbox')->putFileAs(
                'challenge-files/' . $name . '/',
                $this->file->file,
                $this->file->original_name
            );

            Log::info('message: ' . $rez);
        } else {
            Log::alert('falseeeee');
        }
}

FilesystemAdapter puthFileAs 方法:

public function putFileAs($path, $file, $name, $options = [])
{
        $stream = fopen(is_string($file) ? $file : $file->getRealPath(), 'r');

        // Next, we will format the path of the file and store the file using a stream since
        // they provide better performance than alternatives. Once we write the file this
        // stream will get closed automatically by us so the developer doesn't have to.
        $result = $this->put(
            $path = trim($path.'/'.$name, '/'), $stream, $options
        );

        if (is_resource($stream)) {
            fclose($stream);
        }

        return $result ? $path : false;
}

filesystems.php 本地磁盘配置

'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'permissions' => [
                'file' => [
                    'public' => 0664,
                    'private' => 0600,
                ],
                'dir' => [
                    'public' => 0775,
                    'private' => 0700,
                ],
            ],
],

【问题讨论】:

    标签: php laravel file storage fopen


    【解决方案1】:

    可能您没有修改文件系统配置文件中的权限映射。

    寻找

    目录

    数组并检查

    公开

    数字是

    0775

    如果不是,则将其更改为该数字 寻找

    文件

    更改'public' => 0664

    【讨论】:

    • 你用的是什么版本的 laravel?
    • 我发现了问题。问题是路径是相对的。添加“存储/应用程序/”后,它起作用了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2013-01-02
    • 2019-02-11
    • 2012-10-31
    • 2022-01-11
    相关资源
    最近更新 更多