SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Cannot navigate away while downloading a file

    While a user is downloading a file from my site, they cannot navigate to another page until the download completes or is cancelled. How do I fix this?

    Here is my download script:

    PHP Code:
    <?php

    //Connect to server
    require_once('connect.php');

    //Select database
    mysql_select_db($database_****, $****);

    //Start session
    session_start();

    //Include external functions
    require_once('functions.php');

    //Define submission ID variable
    $submission_id mysql_real_escape_string($_GET['id']);

    //Check if submission is valid
    if(valid_submission_id($submission_id) == TRUE) {

        
    //Define Submission Data query
        
    $query_submission_data mysql_query("SELECT id, source, size, file_type FROM submission_data WHERE id = '$submission_id'") or die(mysql_error());

        
    //Define Submission Data array
        
    $row_submission_data mysql_fetch_assoc($query_submission_data);

        
    //Set headers
        
    header('Content-Type: '.$row_submission_data['file_type']);
        
    header('Content-Disposition: attachment; filename="'.substr($row_submission_data['source'], 13).'"');
        
    header('Content-Length: '.$row_submission_data['size']);
        
    readfile('../'.$row_submission_data['source']);
    }
        
    //Redirect to previous page
    header('location: '.$_SESSION['page']);
    ignore_user_abort(true);
    session_write_close();
        
    ?>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You should call session_write_close() before sending the file, not at the end of the script. The session is automatically closed at the end of the script already, your purpose in putting it there was probably to close the session early so that the user could continue using the site.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You should call session_write_close() before sending the file, not at the end of the script. The session is automatically closed at the end of the script already, your purpose in putting it there was probably to close the session early so that the user could continue using the site.
    Works perfect. Thanks! =)


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
  •