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 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File Download Problem

    I recently created a script that creates a txt file in a randomly named folder within a specified folder. I would like to know how I can make this txt file automatically download...?

    I have heard of placing the URL within an iframe or even using a meta refresh tag to download it, but since I'm working with a .txt file, by default it would load the file within the browser instead of present the user with a downloadable file. This is an inconvenience to a lot of people because then they have to manually save the file (I wouldn't mind, but a lot of other people would). By the way, the download link is parsed with PHP. Not sure if that matters.

    Is there something I can do with .htaccess to force the file to be downloaded?

    Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I believe its how you setup your headers?

    header('Content-Disposition: attachment; filename="downloaded.pdf"');

    Not sure but I 'think' thats how you force a download when a link is clicked.

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would I go about implementing that with my links?

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Throw up some code.

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    :P Nevermind. I figured out that I could implement that information into my .HTACCESS file:

    <Files *.txt>
    ForceType application/octet-stream
    Header set Content-Disposition attachment
    </Files>


    Thanks for your help.

  • #6
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    LoL the php version is almost the same

    PHP Code:
    <?php

    header
    ("Content-Type: application/octet-stream");
    header("Content-Length: " filesize("filename.suffix"));
    header('Content-Disposition: attachment; filename="filename.suffix"');

    readfile("filename.suffix");

    ?>
    Set that up as a php page and use it for all your txt files dynamically, OR w/e else you want to download.

  • #7
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just figured I would use the .htaccess and have the script copy the .htaccess to the random directory that is created upon the script's execution.

    It seems to do just fine. Thanks again.


  •  

    Posting Permissions

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