0

My goal is to upload a large file to dropbox and because the waiting time can be too long I want to split the process and upload the file through a queue.

I'm doing the following:

  • I upload a file (that can be large)

  • I save it on local storage

  • I save data about the file in database.
  • In a queue I want to get the file and move it to a dropbox disk.

The problem is that when I do the last step I get the following error

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

I tried different approaches but I can't find a solution.

My code

Controller method:

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']);
        }
}

Queue job:

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 method:

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 disk configs

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

1 Answers1

0

Probably, you haven't modify the permissions mappings in your filesystems configuration file.

Look for the

dir

array and check if the

public

number is

0775

if it is not, change it to that number Look for

file

change 'public' => 0664

gbenga ogunbule
  • 589
  • 1
  • 6
  • 15