Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts

    Force download of text file

    I should preface this with: I'm an ASP developer, my PHP is dodgy at best and the following script is cobbled together from bits I've found online.

    The script reads an encrypted text file, decrypts it, and SHOULD force the browser to save the decrypted data as a plain text file. It does everything right except the last bit. If I comment the header lines out (as I have in the code below), it writes the decrypted text to the browser.

    If I leave them in, Firefox hangs for a bit and then tells me it had an error downloading the file.

    I'm pretty sure this is a simple thing - I'm not writing the right headers, or something. But my PHP experience is not up to giving me a clue what.

    Any ideas?

    PHP Code:
    <?php

    include "file_func.php";


    $key 'my secret key';
    $iv '12345678';

    $cipher mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');

    $filename $_GET["file"];
    $filepath "../_secrethiddenfolder/".$filename;

    $data=read_file($filepath);

    mcrypt_generic_init($cipher$key$iv);
    $decrypted mdecrypt_generic($cipher,$data);
    mcrypt_generic_deinit($cipher);


    $decrypted str_replace("---""\n"$decrypted);

    $filesize strlen($decrypted); 



    ob_clean_all();

    /*
    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-Transfer-Encoding: binary");
    header("Content-Type: application/octet-stream");
    header("Content-Length: " . $filesize);
    header("Content-Disposition: attachment; filename=\"" . $filename . "\";" ); 
    */

    echo rtrim($decrypted"\0");

    //echo $filesize;

    die(); 


    function 
    ob_clean_all () {
        
    $ob_active ob_get_length () !== false;
        while(
    $ob_active) {
            
    ob_end_clean();
            
    $ob_active ob_get_length () !== false;
        }
        return 
    true;
    }


    ?>

  • #2
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    http://www.sephiroth.it/phpwiki/inde...oad_Text_files

    I've been meaning to find a solution to this same problem for a while now, and this reminded me.

    I tested the above in IE7, Firefox and Opera and they all worked fine. The example doesn't set $filename, so don't forget that. Also, that example uses short tags, you may need to replace the <? with <?php.

  • #3
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Well, I got an error on the "PMA_USR_BROWSER_AGENT" bit, so I took all the browser-specific stuff out and just used the non-IE code, so I could see if it worked in Firefox. Which it did. I then hit it with IE7 and the same code works fine. So thank you very much

  • #4
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    I have a script that I use to force download all kinds of files. You may find it of use to you in this situation. Hope it helps.
    PHP Code:
    <?php

    $filename 
    $_GET['file'];

    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression'))
      
    ini_set('zlib.output_compression''Off');

    // addition by Jorg Weske
    $file_extension strtolower(substr(strrchr($filename,"."),1));

    if( 
    $filename == "" 
    {
      echo 
    "<html><title>Title Here</title><body>ERROR: download file NOT SPECIFIED. </body></html>";
      exit;
    } elseif ( ! 
    file_exists$filename ) ) 
    {
      echo 
    "<html><title>Title Here</title><body>ERROR: File not found.</body></html>";
      exit;
    };
    switch( 
    $file_extension )
    {
      case 
    "pdf"$ctype="application/pdf"; break;
      case 
    "exe"$ctype="application/octet-stream"; break;
      case 
    "zip"$ctype="application/zip"; break;
      case 
    "doc"$ctype="application/msword"; break;
      case 
    "xls"$ctype="application/vnd.ms-excel"; break;
      case 
    "ppt"$ctype="application/vnd.ms-powerpoint"; break;
      case 
    "gif"$ctype="image/gif"; break;
      case 
    "png"$ctype="image/png"; break;
      case 
    "jpeg":
      case 
    "jpg"$ctype="image/jpg"; break;
      default: 
    $ctype="application/force-download";
    }
    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: $ctype");
    // change, added quotes to allow spaces in filenames, by Rajkumar Singh
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");
    exit();

    ?>
    And then when I want to a link to download a file I use

    Code:
    <a href="http://www.domain.com/download.php?file=filename.pdf">download pdf</a>
    And make sure this file is saved as download.php and in the same directory as the file you want to download.


  •  

    Posting Permissions

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