SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Autodownload of just written file

    Suppose I use PHP to write out a bunch of information to a flat file, either plain text or comma delimited. Now, I want the user to be able to download that file to their machine right after PHP has created it. How do I give the download prompt for the file? I don't want the contains of the document displayed on a web page. I want them to actually get a download dialog box to get the file themselves.

    Any help you can provide would be greatly appreciated.

  2. #2

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this should do the trick
    PHP Code:
    header('Content-Length: FILESIZE');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="SUGGESTED_FILENAME"');
    readfile('THE_FILE'); 

  3. #3
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works perfectly. Thank you!

  4. #4
    SitePoint Guru JoePrice's Avatar
    Join Date
    Jan 2002
    Location
    UK
    Posts
    673
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there

    Re the code above,
    how do you associate that code to a hyperlink?

  5. #5
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BuzzFuttock
    Hi there

    Re the code above,
    how do you associate that code to a hyperlink?
    Technically, but technically not.

    When a user clicks on a hyperlink, or posts a form, they are taken to the above page (say, output.php), which contains the above code.

    It then instructs the browser, not to display the output, but to prompt the user to download it.

    Thus, the code above pertains to the target page, whether that be a form's action="..." statement, or a hyperlink.
    My name is Steve, and I'm a super-villian.

  6. #6
    SitePoint Guru JoePrice's Avatar
    Join Date
    Jan 2002
    Location
    UK
    Posts
    673
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As an extension to this thread.

    The code works perfectly.
    However, when the user clicks on the link, I want :
    - a new window to open up with the script in
    - do the download box thing
    - and then close the newly opened window.

    I can't get the newly opened window to close.
    I've added the following statement beneath the
    readfile statement in the code above:

    Code:
    echo("<script language='javascript'>window.close()</script>");

    Any suggestions?

    Ideally I wouldnt want a new page opened up at all, however I can't see how I can do this since I have to point to the php script above.


    many thanks for any ideas

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Southern Maine
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would this code:

    PHP Code:
    header('Content-Length: FILESIZE'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="SUGGESTED_FILENAME"'); 
    readfile('THE_FILE'); 
    Go in the head of the document or anywhere?
    I was looking for something like this, for a php color syntax writer.

    Thanks.
    Working on a new CSS site for RKS and PHP driven?

    Looking for staff, tutorial site for coding, and photshop. PM me.

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Southern Maine
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BuzzFuttock: did you try javascript for the box?
    Working on a new CSS site for RKS and PHP driven?

    Looking for staff, tutorial site for coding, and photshop. PM me.


Bookmarks

Posting Permissions

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