Secure download problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shnizle
    New Member
    • Apr 2009
    • 16

    Secure download problem.

    hi everyone,

    i am using the following code to make a secure download in my site:

    Code:
    <?
    include "common.php";
    commonConnectToDB();
    
    $sql = "select * from contacts where now()-insertTime < 180";
    $res = commonDoQuery($sql);
    if (mysql_num_rows($res) == 0)
    		return;
    
    $asfname = "MyFileName.exe";
    $filename = commonGetLayoutSwitchHtml("trialDownloadFileName", "ENG");
    $realFileName = "loadedFiles/$filename";
    $fsize = filesize($realFileName); 
    
    /* waiting for PHP 5.3
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mtype = finfo_file($finfo, $filename);
    finfo_close($finfo);
     */
    
    $mtype = "application/exe";
    
    // set headers
    header("Pragma: no-cache");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Description: File Transfer");
    header("Content-Length: $fsize");
    header("Content-Type: $mtype");
    header("Content-Disposition: attachment; filename=\"$asfname\"");
    header("Content-Transfer-Encoding: binary");
    //flush();
    
    $file = @fopen($realFileName,"rb");
    if ($file) {
      while(!feof($file)) {
        print(fread($file, 1024*8));
        flush();
        if (connection_status()!=0) {
          @fclose($file);
          die();
        }
      }
      @fclose($file);
    }
    ?>
    everything seems to work, except when i download the file,
    i have a "unknown time remaining" text on the download,
    for some reason the browser does not know the size of the file , even though i have set the headers correctly.

    any idea what i'm doing wrong here?
  • gopher83
    New Member
    • Feb 2010
    • 2

    #2
    I tried this script, and worked properly at my localhost (php5.3, apache, win7) with a 150MB zip.

    Comment

    Working...