1

Overview

I would like to push the current file upload process to a queue. When the file upload has completed, an email will be sent out to the user that the upload process complete. This idea has been implemented because large sets of files will be uploaded and we would like the user to navigate to other parts of the system while the upload process is being carried out.

What's in place right now !

Currently I am iterating through all the files that are meant to be uploaded (in my controller ) and creating an array of the uploaded files. I am doing this because Laravel does not allow me to send models to the queue. Here is the code

if (Input::hasFile('files')) {

                $filesarr = Input::file('files');

                foreach($filesarr as $fileitem)
                {

                    array_push($filesArray, array(
                            'realpath'=>$fileitem->getRealPath(),
                            'originalname' => $fileitem->getClientOriginalName(),
                            'originalextension'=>$fileitem->getClientOriginalExtension(),
                            'mime' =>$fileitem->getClientMimeType(),
                            'size' =>$fileitem->getClientSize(),
                            'maxFileSizeAllowed' => $fileitem ->getMaxFileSize(),
                            'isUpdateMode' => 1,
                            'orealPath' => $fileitem->getOriginalPath()
                        ));
                }
            }

I am then sending this array to the queue.

Queue::push('TopicQueue', array ('dataArr' =>$filesArray));

In my queue fire method, I am getting the array of uploaded files. The code follows

$files = $dataArray['$filesArray'];

Now, I am trying to use laravel method of file uploading , so again, I try to create an instance of Laravel's UploadFile class. The code for that follows

 foreach($allFiles as $file)
            {
                Log::info("File is :" .var_export($file,true));
                Log::info('Going to upload file : '.$file['originalname']);
                try
                {
                    $uploadFile = new UploadedFile($file['realpath'],$file['originalname'],$file['mime'],$file['size']);
                }
                catch(Exception $ex)
                {
                    Log::info('The error is : '.$ex->getMessage());
                }
}

but here I am encountering an issue (which is correct) . The instantiation fails with the message

The file does not exist.

This is happening within the constructor of the uploadfile class itself.

I also tried to searlize and unserialize the class, but apparently the UploadFile is not serializable.

Someone care to help, please !

Thanks

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Gagan
  • 5,416
  • 13
  • 58
  • 86

1 Answers1

4

Its not possible to do what you want.

The user needs to complete the file upload in the $POST request of the form - you cant 'queue' it to be done later. Queueing is for server side processing tasks to be delayed and done later - but uploading needs the user to stay on the page to 'send' the data to your server.

To further expand - the best option you will be able to do is a javascript asysncronous upload - using a package like dropzonejs or something. This way users can upload multiple files simulatenously, and get visual progression bars updating.

One option might be to have the 'upload' form open in a new window - so the users can 'leave' that window alone while they browse on another window waiting for the uploads.

Finally - this link might also help: How can I upload files asynchronously?

Community
  • 1
  • 1
Laurence
  • 58,936
  • 21
  • 171
  • 212