Friday, March 31, 2017

Continue PHP Operation After User Exits Early

I'm using Laravel, PHP7, PHP-FPM, APCu and NGINX.

I have an HTML form where a user can upload a file, it connects to Upload.php.

File Process:

  1. validate
  2. name
  3. create thumbnail
  4. move from /tmp to /media
  5. create database record

Once the PHP script reaches a certain point, how can I have it continue running even if the user exits the upload page early? Or else a rogue file will be left in the directory without a database entry.

// Move uploaded file from /tmp to /media
Input::file('file')->move("/var/www/mysite/media", $image);

// Continue even if user exits early
// prevent a file in /media from not having a database record

// Thumbnail creation and other operations here
// May take several seconds

// Save database record
$image = new Gallery();
$image->name = $name;
$image->created_at = $date;
$image->save();

Should I use ignore_user_abort(true) and wrap the operations in a while(true)?

I have other bools in the script such as $upload = true. How does the while(true) know to represent ignore_user_about(true) and not another bool I have set?



via Matt McManis

Advertisement