Laravel-如何将文件从本地存储移动到另一个存储?

问题描述 投票:0回答:1

我的目标是将一个大文件上传到Dropbox,并且由于等待时间可能太长,所以我想拆分进程并通过队列上传文件。

我正在执行以下操作:

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

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

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

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

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个回答
0
投票
寻找

dir

数组并检查是否

公开

数字为

0775

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

文件

更改'public' => 0664
© www.soinside.com 2019 - 2024. All rights reserved.