Results 1 to 19 of 19

Thread: Getting the extension right on a download [resolved]

  1. #1

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    Getting the extension right on a download [resolved]

    I'm using this code to download a file:
    PHP Code:
    <?
    //Download a file
    $file = $_GET['file'];
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-type: application/x-zip");
    header("Content-disposition: attachment; filename=".$file);
    readfile($file);
    ?>
    I have to use this so that .html files can be downloaded and not simply viewed. I've noticed thought that in IE there is no extension to the file. in FireFox it gives the corrrect file xtension, but not in IE. I've even tried reading the user agent and manually entering an extension for IE. Still didn't work.

    Actually, the file even has different names in the two browsers, neither of which are the name of the real file, they still download correctly though.

    Can someone please tell me why IE doesn't give an extension and how to give it one.
    Last edited by Acidic; May 2nd, 2004 at 09:16 AM.
    Have I helped you? Please Rate my posts.

  2. #2
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    I use this:
    PHP Code:
    $FName substr($File,strrchr($File,"/"));

    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=$FName");
    header("Location: $File"); 
    This works in IE haven't tried it in anything else but nobodies complained about it not working in FireFox that I know people have used with it.$File is the Full URL of the file to be downloaded. I notice you are using Attachemnet and I am using inline though. You might notice though the Full URL in mine is shaved down to only the filename for the filename.
    Thing is I'm using the Location thing instead of the ReadFile(). There is meant to be some problems when using the ReadFile() with IE mind.
    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.

  3. #3

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    What have I done wrong?
    here's the URL I go to:
    'download.php?File=www.horspool.org.uk/sid/JavaScript/".$pre.$a.".html'

    it says it cannot fomd the document. and the URL becomes:
    http://www.horspool.org.uk/sid/www.h...ript/0002.html
    Have I helped you? Please Rate my posts.

  4. #4

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    ok. now it just loads up the page, in both browsers.

    I used EXACTLY the code you gave me, pasted that into download.php

    then here's the link which goes to it:
    PHP Code:
    print "<input type='button' value='Get Script' onClick=\"goto_URL('download.php?File=http://www.horspool.org.uk/sid/JavaScript/".$pre.$a.".html')\">"
    Have I helped you? Please Rate my posts.

  5. #5
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Posted by Acidic
    What have I done wrong?
    here's the URL I go to:
    'download.php?File=www.horspool.org.uk/sid/JavaScript/".$pre.$a.".html'

    it says it cannot fomd the document. and the URL becomes:
    http://www.horspool.org.uk/sid/www.h...ript/0002.html
    What does $pre & $a hold?

    Also You realise you have single quotes then are cloing them using double quotes and reopening it with double and closing with single.

    I would have though you would have ended up with:
    http://www.horspool.org.uk/sid/www.h...id/JavaScript/"0002".html

    Are you using the header("Location: $File"); thing now? Otherwise its not going to amke a differeance.

    The file its looking for is: http://www.horspool.org.uk/sid/www.h...ript/0002.html
    Which woundn't exist. I don't know if its making it a relitive path unless you include the http:// in it as well. I do in mine. Mind I dont pass mine as a parameter in the URL I pass a file ID in the URL and then look it up in a database, that way I can log dowloads and make sure only files I want people to download can be downloaded.
    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
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Originally posted by Electroman
    What does $pre & $a hold?
    $pre = "000"
    $a = 2
    ... at least in this case. They are not what's wrong.
    Originally posted by Electroman

    Also You realise you have single quotes then are cloing them using double quotes and reopening it with double and closing with single.
    I think they are working correctly. If you're referring to:
    Code:
    'download.php?File=http://www.horspool.org.uk/sid/JavaScript/".$pre.$a.".html'
    The single qoute is there so the JavaScript function goto_URL knows it's a string, not a variable. The double qoutes are there to open and close the PHP bit. It's very difficult to explain, but I think it's right.

    Originally posted by Electroman

    I would have though you would have ended up with:
    http://www.horspool.org.uk/sid/www.h...id/JavaScript/"0002".html
    that's exactly what I get
    Originally posted by Electroman

    Are you using the header("Location: $File"); thing now? Otherwise its not going to amke a differeance.
    yes: header("Location: $File");


    Originally posted by Electroman

    The file its looking for is: http://www.horspool.org.uk/sid/www.h...ript/0002.html
    Which woundn't exist. I don't know if its making it a relitive path unless you include the http:// in it as well. I do in mine.
    When I do include the "http://". It goes to the file, when I don't include it, it makes a relative path, to a file which doesn't exist.
    Have I helped you? Please Rate my posts.

  7. #7
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Try using Content-disposition: attachment when using the http:// so that it was going to the page when you were using Content-disposition: inline. Also I think you'll need to change the Content-Type, but dont worry about that just yet.
    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.

  8. #8

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    it's now:
    PHP Code:
    <?
    $FName = substr($File,strrchr($File,"/"));

    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: attachment ; filename=$FName");
    header("Location: $File");
    ?>
    and I'm using "http://" in my link. and it goes to the file, doesn't download it.
    Have I helped you? Please Rate my posts.

  9. #9
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Try:
    PHP Code:
    <? 
    $FName = substr($File,strrchr($File,"/")); 

    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/octet-stream"); 
    header("Content-disposition: attachment ; filename=$FName"); 
    header("Location: $File"); 
    ?>
    I'm not sure if it will work but I'm nearly sure the problem will lie with the Mine Type.
    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.

  10. #10
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    You might want to check this out too: http://ppewww.ph.gla.ac.uk/~flavell/...tent-type.html
    At the very bottom it mentions that if you set the Content-type to text/plain when its the results of a cgi script then IE will decide to download the file instead of display it. that could help you when the user is using IE. Dont know if php would count as cgi in this case though, might just be perl or such.
    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.

  11. #11

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    It still goes there.
    Have I helped you? Please Rate my posts.

  12. #12

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    whoaa. I changed it back to readfile($File) and it worked in both browsers. You're right, MIME type did it.

    IE names the file download and FX gives it the name of the URL. But that's something for me to wrry about later. Thanks.

    Edit: thanks for the link, I'll have a look.
    Have I helped you? Please Rate my posts.

  13. #13
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    How about other browsers, do they act as expected?
    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.

  14. #14
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Originally posted by Acidic
    whoaa. I changed it back to readfile($File) and it worked in both browsers. You're right, MIME type did it.

    IE names the file download and FX gives it the name of the URL. But that's something for me to wrry about later. Thanks.

    Edit: thanks for the link, I'll have a look.
    Cool , which Mime type worked in the end?
    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.

  15. #15

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    application/octet-stream
    Have I helped you? Please Rate my posts.

  16. #16
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Posted by Acidic
    application/octet-stream
    Did you check the downloaded result looked ok in notepad? Just becuase the NewLine Charaters may be messed up becuase were downloading it in binary, hopefuly not though.
    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.

  17. #17

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Originally posted by Electroman
    Did you check the downloaded result looked ok in notepad? Just becuase the NewLine Charaters may be messed up becuase were downloading it in binary, hopefuly not though.
    No it doesn't do NewLine, it places one of those thingies there (same thing you got when downloading NoteMe's CSS file). But any editor other than Notepad, ie. one that understands ASCII will display it correctly. I'll leave it as it is. at least for now.
    Have I helped you? Please Rate my posts.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    "Any other editor" is one that understands Unix line endings. Nothing to do with ASCII.
    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.

  19. #19

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    OK, note taken. Thanks.
    Have I helped you? Please Rate my posts.

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