SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy simple downloads !!

    hi there

    newbie here, i am getting there now with my first php site and now i am completely baffled on how to make a simple function for downloading files using a button, or such like. i don't want to out date the site with right clicking every file that comes up, does anyone have an idea of where to point me in the right direction, of course google was a bundle of help.

    ashley

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you google for
    php force download

    you should get some relevant results.
    you may also want to see the documentation for header() and readfile(), as well as the user comments.

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; $filename");
    header("Content-Length: ".filesize($filename)); 
    readfile($filename); 
    Saul

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    thanks for the replies, just one question now, if i was to have an img as a button then how would i include the php to work off the img, would i insert the php code between the img tag.

    regards

    ashley

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, the image or a text link directs the browser to the given url, that's where your script is. For example:
    HTML Code:
    <a href="download.php?id=12324"><img src="download.gif" /></a>
    download.php is your script handling the download.
    Saul

  6. #6
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks again

    thanks for the reply once again, i am almost there now, i am hoping to put a few files up from one folder so i will put them all into a php file, in each bit off code though will i include the id in that code, how do i id a php bit of code, i am new to this so apologies if it seems a dumb question.

    regards

    ashley

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I gave the id as an example if you used a database to store the file names. But if you don't, the id can be a file name. So if you have some files in directory named 'files':
    PHP Code:
    $filename=$_GET['id'];
    if(!
    preg_match("/^[a-z0-9_-]+\.[a-z0-9_-]+$/i",$filename)) exit;
    else{
      
    header("Content-Type: application/download");
      
    header("Content-Disposition: attachment; files/$filename");
      
    header("Content-Length: ".filesize("files/$filename"));
      
    readfile("files/$filename"); 

    And
    HTML Code:
    <a href="download.php?id=filename.ext"><img src="download.gif" /></a>
    Note that preg_match limits the file name to alphanumerics, underscore and dash for security purposes.
    Saul

  8. #8
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I prefer a manual download button rather than clicking on a link that sends me to a page that automatically starts a transfer. Some sites I've visited are a pain, you decide not to download the file, hit the back button, and well, it starts another transfer. For the novice, they don't think about double clicking the back button or using their history to go back to the original "file description page." and would tend to close out their browser or tab for the site when encountering this.
    intragenesis, llc professional web & graphic design

  9. #9
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    now i get it

    thanks very much for the reply i get how it works now, completely. could holmescreek tell me what a manual download button is exactly.

    reagrds

    ashley

  10. #10
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Display a .gif file with "Download This File Now" that links to the actual file to be downloaded rather than linking to a redirected page that starts an auto transfer.

    I ran into (as I described) even today. Went to a site to check out a file, when I clicked on the description of the file, it automatically started the download. I was expecting to click a link to a page with more information. Bad problem was, whenever I hit the back button, it would start the file download again...and again... ugh!

    Just saying a nice .GIF or even text link that states "Download This File Now" that links directly to the file would eliminate a user getting stuck into a page that starts an auto-download process.
    intragenesis, llc professional web & graphic design

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    most likely they were using a meta refresh(with a delay) to do the redirect, if it was breaking your back button so to speak. an http header redirect shouldnt do that, you browser should skip back to the file before you got redirected, not skip back to the url which issued the redirect header.

    regardless, i agree. id prefer a link, or a link that looks like a button, to lead directly to the download file.

  12. #12
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, I have redirects on some of my clients sites for banner ads, but, in that case, I just prefer that the visitor click on a banner ad an a new window open. Thus, they can close out the window when done viewing what they were interested in -- and best of all, they are still sitting on my clients site. ( get em' there -- keep em' there)
    intragenesis, llc professional web & graphic design

  13. #13
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks to everyone

    thanks for all the information i will have a go now, just as a gesture this my mates site that i am going to improve, it is so outdated it might give you a little perk up.

    http://dhost.info/tabs/inex.html

    regards

    ashley

  14. #14
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorry wrong address

    heres the correct address

    http://dhost.info/tabs/index.html


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
  •