Results 1 to 17 of 17

Thread: Sending a file [Resolved]

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Sending a file [Resolved]

    How can I send a file using php, for example say how images get sent by something like:
    PHP Code:
    header("Content-type: image/png");
    ..... 
    Is there a way to send a header telling the browser its about to recieve a file for downloading.

    Thanx
    Last edited by Electroman; Feb 27th, 2004 at 06:44 AM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I just tried this:

    Code:
    header('Content-type: image/gif');
    header('Content-Disposition: attachment; filename="image.gif"');
    readfile('image.gif');
    And it popped up the download dialog. Is that what you want?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Kind of but I'm more after being able to get it to download Zip files.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    For a zip file, just do:

    Code:
    header('Location: http://www.site.com/location/of/zip.zip');
    And it will prompt to download the zip file.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Thanx, I'll give it a go.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  6. #6

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Humm, this was strange, I just made that change and when I requested a file it displayed the file in text as garbage. However just as I was about to post a reply I tired it one last time and it worked. I hadn't even edited the file . Any how it works now so thank you very much.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    if you are going to send a file in the header then you should be using these as well,

    header("Expires: Mon, 26 Nov 1962 00:00:00 GMT");
    header("Last-Modified: " . gmdate("D,d M Y H:i:s") . " GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-type: application/x-zip");
    header("Content-disposition: inline; filename=zip.zip);
    header('Location: http://www.site.com/location/of/zip.zip');

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Why?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    because then this doesn't happen. you have to get the browser prepared to send it like that. it isn't like a link.

    Originally posted by Electroman
    Humm, this was strange, I just made that change and when I requested a file it displayed the file in text as garbage.

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I've never had any problem just doing header('Location: url'); before. If you could point me into the direction of some documentation, that'd be great.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    I am surprised you haven't as there are way to many bugs in IE and other browsers to get it right.

    http://us2.php.net/manual/en/function.header.php

    read through the comments and you will see some of the bugs and why those headers are important.

    sending the zip file in the header and having php render the page as text/html and not the application/zip is what causes most of the problems.

    the "Expires and Last-Modified" may not be as important but I just had them in my notes so I posted them as well.

  12. #12

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Reading that page would it not be better using:
    readfile('http://www.site.com/location/of/zip.zip')
    instead of:
    header('Location: http://www.site.com/location/of/zip.zip')

    It mentions problem with old browsers tho, any idea what happens when these problems strike?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  13. #13
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you can use readfile if you are doing it that way. and what happens? well you experienced it yourslef, it loads the text in the browser. but that is only 1 of many bugs. I can't remeber them all.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    For general binary data, use a application/octet-stream MIME type (Cotent-type). For others, you should look up the MIME type. Don't know where to best look for it though.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    That's all jabberwocky to me.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Then you'd best read up on the HTTP protocol...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by CornedBee
    Then you'd best read up on the HTTP protocol...
    No. I refuse to learn.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width