Results 1 to 25 of 25

Thread: How can download a file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    How can download a file

    Hello
    How can I download a file in PHP?
    I want when I click on submit button, it shows download dialog.

    How?
    Thanks

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    to force a save dialogue:

    PHP Code:
    <?php
      
    // We'll be outputting a PDF
      
    header('Content-type: application/pdf');

      
    // It will be called downloaded.pdf
      
    header('Content-Disposition: attachment; filename="downloaded.pdf"');

      
    // The PDF source is in original.pdf
      
    readfile('original.pdf');
    ?>
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    OK, what if it's "zip" or "exe" file?

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    you should try looking up mime types, to make sure. there's a large list for application mime types here, so take your pick.

    The filename tag under "Content-Disposition" is a recommended filename, and is what will show up in the user's save dialogue. The readfile() functionis the actual file path to the file the user is requesting.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    It didn't work.
    Actually its size is 7 M.B, but it said that its size is 1.90 M.B and the icon of the file is unknown.
    Why?

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    .. post your code. I can't help you if I don't know what you're doing.

    and just so you know, you don't need to do this to download an exe or zip file.. just make a link to them, the browser will automatically prompt to download..
    Like Archer? Check out some Sterling Archer quotes.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    PHP Code:
    header('Content-type: application/exe'); 
    header('Content-Disposition: attachment; filename="file.exe"'); 
    readfile(file.exe'); 
    Look at the dialog, there is something wrong.
    Attached Images Attached Images  

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How can download a file

    What is wrong? The browser cannot know the icon of the exe, since it hasn't started downloading it yet.
    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.

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    all EXEs will show up with that icon when downloading, because the icon for that file is embedded in the exe.. and since they haven't downloaded the exe yet, it won't show up, just like CornedBee said.

    however, you might be able to assign an icon file on your server to that file, but I have no idea how you'd go about doing it, and it has nothing to do with PHP, but rather how the web server is configured.
    Like Archer? Check out some Sterling Archer quotes.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    But after downloading, the file isn't working.

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    then it probably has something to do with the exe, and not the script. it might be corrupt, or you might not have the dependencies installed that it needs to run.

    I used the same script and the file I downloaded ran fine.

    PHP Code:
    <?
      header('Content-type: application/exe'); 
      header('Content-Disposition: attachment; filename="suggestedfilename.exe"'); 
      readfile('filename.exe');
    ?>
    Like Archer? Check out some Sterling Archer quotes.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    I also tried zip files.
    It isn't working after I'd downloaded it.
    And the file is not corrupted because when I download it classically, it's working.

  13. #13
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    post the code you're using?
    Like Archer? Check out some Sterling Archer quotes.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    PHP Code:
    header('Content-type: application/zip');  
    header('Content-Disposition: attachment; filename="suggestedfilename.zip"');  
    readfile('filename.zip'); 

  15. #15
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    try using the absolute file path to the file you want? ie: /home/user/files/filename.zip (unix) or C:/filename.zip (windows)

    The code is fine, so unless you have more code that might be making it not work, I'm not sure what to tell you.
    Like Archer? Check out some Sterling Archer quotes.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    Same problem

  17. #17
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    then it's something to do with your browser or the server, I'm guessing.
    Like Archer? Check out some Sterling Archer quotes.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    There is a code I've just found, it's
    PHP Code:
    header ("Location: file.exe"); 
    And it worked.
    What the difference between it and the code you gave to me.

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How can download a file

    The code just forwards the browser to the actual location of the server-side file. It won't work if that location is not directly accessible or if you want to generate the file on the fly.
    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.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    I didn't understand.
    Can you explain more for me pleeeeeeease?

  21. #21
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    What you pasted just redirects the page to that file, and is only accessible if you have it available on your HTTP server. However, using the method I posted, it's possible for you to download files on the server, but not in the HTTP directories (I believe so, anyway @_@). Basically, the script you made is nothing more than just like creating a link to the exe file :/
    Like Archer? Check out some Sterling Archer quotes.

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    OK, why didn't the code you posted work?
    please

  23. #23
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How can download a file

    it worked fine for me. I don't know why it wouldn't work for you. It most likely has something to do with your web server's configuration.
    Like Archer? Check out some Sterling Archer quotes.

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    Does anyone know what I have to do to make it work?

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Location
    United States
    Posts
    157

    Re: How can download a file

    pleeeeeeeease.

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