when downloading a file, I can't navigate to other pages

Discussion in 'PHP' started by SIAWebDesign, Dec 7, 2009.

  1. #1
    Hi All,

    I'm using a download script to download files from my site just so I don't show the file path and don't allow direct downloads.

    Here's the part that does the download after the database and logging stuff are taken care of:

    $speed = 500;
    
    $fsize = filesize($fullPath);
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    //header("Content-Type: ".$mime_type);
    header("Content-Disposition: attachment; filename=\"".$row["file_name"]."\"");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . $fsize);
    
    $file = @fopen($fullPath,"rb");
    if ($file) {
      while(!feof($file)) {
        echo fread($file, round(1024*$speed));
        flush();
        sleep(1);
        if (connection_status()!=0) {
          @fclose($file);
          die();
        }
      }
      @fclose($file);
    }
    Code (markup):
    Now I have two questions/problems:

    1) my speed is set to 500, doesn't that mean max 500KB/sec? why am I only able to download at around 250KB/sec? If I remove the speed limits, then I can download up to 1.5MB/sec.

    2) The main issues with this download script is while a file is being downloaded, I can't navigate to any other page on my site. even if I have 3 or 4 tabs/windows open, none of the links within my site work anymore in any of the other windows until the download is completed.

    I can however, navigate around the internet in other sites, just not my own site.

    I'm pretty sure the issue is my download script because if I allow direct downloads, then I can navigate within the site while the file is being downloaded.

    Thank a lot in advance for all your help.
     
    SIAWebDesign, Dec 7, 2009 IP
  2. SIAWebDesign

    SIAWebDesign Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SIAWebDesign, Dec 7, 2009 IP
  3. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    szalinski, Dec 7, 2009 IP
  4. SIAWebDesign

    SIAWebDesign Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That was exactly my problem. Someone else mentioned it to me already. thank you for the post as well. Now I just have to figure out how to keep my user logged in while he's downloading a file. If I kill the session, the user will be logged out and that's not what I want.

    Thanks again for your reply.

    Cheers
     
    SIAWebDesign, Dec 8, 2009 IP
  5. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I found this immediately, hope it's the solution you need (obviously it might need more research):

    http://www.php.net/manual/en/function.session-write-close.php#54838
     
    szalinski, Dec 8, 2009 IP
  6. SIAWebDesign

    SIAWebDesign Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You are amazing :)

    All I had to call was session_write_close(); instead of session_destroy();

    that way the session stays open and my problem is solved.

    Thank you sooo much.
     
    SIAWebDesign, Dec 8, 2009 IP