SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    handle cancel download

    Hi All,
    I have a script that dynamically creates a bunch of hires jpegs and presents them to the user to download in a nice, easy, managable and lovely zip file (using exec command line function). When the user has downloaded the file all 'temp' images are then cleaned up and the zip file deleted so not to take up any space.
    Problem is that if the user cancels the download the cleanup operation at the end of my script doesnt run - this will lead to problems later on as the next time that i create a zip that has the same filename as one that hasnt been cleaned up, instead of creating a new zip files will just be added to the one that is already on the server and not been deleted.

    How do i handle the cancellation of a forced file download so that i can cleanup images / zips if they do this?

    Here is a snip of my code.

    for(all of my created jpegs){
    $myFiles[] = "design".$des_num."_template".$t_ID.".jpg";
    }
    $execStr = "zip myZipFile.zip " . implode(" ", $myFiles);
    exec($execStr);

    $handle = fopen("myZipFile.zip", "r");
    $contents = fread($handle, filesize("myZipFile.zip"));
    fclose($handle);

    //present the zip
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=myZipFile.zip;" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize("myZipFile.zip"));
    //echo out data
    echo $contents;

    foreach($myFiles as $key => $value){
    unlink("/root/to/file/".$value);
    }
    //delete the zip
    unlink("myZipFile.zip");

    Cheers

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If the zip file already exists (file_exists), delete it before you create it again.

    You can use the file creation or modification time to run a cleanup script on a regular interval as well.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •