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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2003
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post

    Forcing Browser to download a txt file

    Hi

    This is something im really struggling with. I have a txt file called text.txt on my server which I am wanting to force the browser to download. No matter what I try it just displays the contents of the file.

    Can anyone help.
    Phil

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Look at example #1595 on this page of the PHP manual (the header() function):

    http://us2.php.net/header

  • Users who have thanked Fumigator for this post:

    Immulsifier (11-21-2007)

  • #3
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    you can use .htaccess if you don't want php solution.

  • Users who have thanked matak for this post:

    Immulsifier (11-21-2007)

  • #4
    New Coder
    Join Date
    Jul 2003
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Thanks for the help, I got it working in the end with some help from the comments on the php pages. Some of those comments are more useful than the php functions manual to be honest

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    53
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Immulsifier View Post
    Thanks for the help, I got it working in the end with some help from the comments on the php pages. Some of those comments are more useful than the php functions manual to be honest
    Could you post the solution, as I'm curious how to do this.
    -Thanks
    JustBlaze

  • #6
    New Coder
    Join Date
    Jul 2003
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Sure

    Code:
    $filename = "text.txt";
    $filepath = "./".$filename;
    
    header("Cache-control: private");
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . filesize($filepath)); 
    header('Content-Disposition: attachment; filename=' . $filename);
    readfile($filename);


  •  

    Posting Permissions

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