Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP script doesn't finish after large file download
    primarykey
    data
    text
    <p>I'm working on a PHP page for a website that will zip files together and allow the user to download it. The file size of the zip can range anywhere from a couple MB to 100MB. My PHP script creates the zip file in a temporary directory and then writes the file contents out to the browser. Once this is finished, the script updates a download counter in a MySQL database and deletes the zip file from the temp directory.</p> <p>This all works fine until I come across a large zip download that takes longer than 30 seconds. <code>max_execution_time</code> in the php.ini file is set to 30 so this makes sense, but if I try using <code>set_time_limit(0)</code> or change <code>max_execution_time</code> the same result occurs. The zip file successfully downloads from the browser with all the correct files inside, but the scripts seems to stop afterwards because the database isn't updated and the temporary zip file on the server isn't deleted.</p> <p>It's a Linux environment with Apache and PHP 5.2.</p> <p>This website is hosted on GoDaddy so I'm not sure if they have limitations on changing the time limit a script can execute for but basically I'd like to let this particular script run indefinitely until it completes. </p> <p>Any thoughts on why I'm not able to set the time limit or any workarounds?</p> <p>Here's my code:</p> <pre><code>&lt;?php // Don't stop the script if the user // closes the browser ignore_user_abort(true); set_time_limit(0); // Generate random name for ZIP file $zip_file = ""; $characters = "0123456789abcdefghijklmnopqrstuvwxyz"; do { $zip_file = "tmp/"; for ($p = 0; $p &lt; 10; $p++) $zip_file .= $characters[mt_rand(0, strlen($characters))]; $zip_file .= ".zip"; } while (file_exists($zip_file)); // Prepare ZIP file $zip = new ZipArchive(); /* Open and add files to ZIP (this part works fine) . . . */ // Close and save ZIP $zip-&gt;close(); // Check browser connection if (connection_status() == 0) { // Send ZIP header("Content-Type: application/zip"); header("Content-Length: " . filesize($zip_file)); header("Content-Disposition: attachment; filename=Download.zip"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Save and then delete file //readfile($zip_file); if ($file = fopen($zip_file, "r")) { // Set buffer size $buffer_size = 1024 * 8; // While we're still transmitting // send over bytes! while(!feof($file) &amp;&amp; (connection_status() == 0)) { print(fread($file, $buffer_size)); flush(); usleep(10000); //&lt;!-- Download speed cap } // Close file descriptor fclose($file); } } /* Update database download counter if connection_status() == 0 . . . */ // Delete the file unlink($zip_file); ?&gt; </code></pre> <p><strong>UPDATE:</strong> I just tried doing another download from my local web server and bumped up the <code>usleep</code> command to 10000 to slow the download time. Total download time took a little over 1 minute and the database is updated and the file is deleted out of /tmp. My local environment runs EasyPHP with Apache and PHP 5.3 on a Windows 7 box. Seems like this could have something to do with GoDaddy.</p> <p>Also, on both the GoDaddy and local sites, I printed out <code>max_execution_time</code> before and after <code>set_time_limit</code> is called from my script and the results were 30 and 0 respectively so I'm not sure what's happening on the GoDaddy side of things.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload