Simple download script required => download.php?id=1234

Discussion in 'PHP' started by centralexpert, Mar 11, 2008.

  1. #1
    Hello,

    I'm trying to get away from showing the absolute url's of my downloads.

    I have all my download data stored in a mysql db with multiple tables and fields.

    One of which is called 'id' and set to auto_increment.

    What code would I need to use to show my users a url like this:

    http://www.mydomain.com/library/download.php?id=1234

    When they click on a link for a file.

    I have seen it in most CMS's but am having trouble finding it.

    Regards
     
    centralexpert, Mar 11, 2008 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    First, you need to get the variable.

    $id = $_GET['id'];

    Then you inject it into your sql.

    SELECT * FROM table WHERE id='{$_GET['id']}'

    Then you need to fetch the data into an array.

    mysql_fetch_assoc($sql);

    *This is just an explanation, not a step by step of the full script.
     
    Kaizoku, Mar 11, 2008 IP
  3. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank You.

    I think I'll have enough to go on from there.

    After all, if someone does it for you all the time, how would we ever learn.

    Thanks Again!:)
     
    centralexpert, Mar 11, 2008 IP
  4. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry to double post.

    I've used the method you suggested, but i'm not sure on how to get the file to appear as a download.

    Thats the only bit i'm stuck on.

    Reagrds
     
    centralexpert, Mar 11, 2008 IP
  5. bpasc95

    bpasc95 Active Member

    Messages:
    196
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Another option is to get the file name out of the DB, then use a header redirect to the file.

    <?php
    ..your db logic getting the file name for $_GET['id']
    $filename = ...the above result...;
    
    header("Location: http://theurl.to/thefiles/".$filename,true,307);
    
    
    PHP:
    With this, you could add some logic to also count the number of downloads as well.

    -Bing
     
    bpasc95, Mar 11, 2008 IP
  6. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK so this is what I have so far

    <?php
    
    $id = $_GET['id'];
    
    include("mydbconnect.php");
    
    mysql_select_db("my_db", $con);
    
    $result = mysql_query("SELECT url FROM downloads WHERE id='{$_GET['id']}'");
    
    $filename = *What Goes Here?*
    
    header("Location: http://mydomain.com/download/path/".$filename,true,307);
    
    mysql_close($con);
    
    ?>
    Code (markup):
    Can you advise me on what I'm missing?

    Thanks
     
    centralexpert, Mar 11, 2008 IP
  7. vishnups

    vishnups Banned

    Messages:
    166
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you are interested I can do it for you. I have used it in my website...If interested PM me...
     
    vishnups, Mar 11, 2008 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    
    <?php
    
    $id = $_GET['id'];
    
    include("mydbconnect.php");
    
    mysql_select_db("my_db", $con);
    
    // LIMIT it, so less strain on the mysql server
    $result = mysql_query("SELECT url FROM downloads WHERE id='{$id}' LIMIT 1");
    
    //$filename = *What Goes Here?*
    $row = mysql_fetch_assoc($result);
    $filename = $row['url'];
    
    header("Location: http://mydomain.com/download/path/".$filename,true,307);
    
    mysql_close($con);
    
    ?>
    
    PHP:
     
    Kaizoku, Mar 11, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Just a small change I'd make:
    
    $id = intval($_GET['id']);
    
    PHP:
    And furthermore, I'd do:
    
    readfile($filename); 
    
    PHP:
    ... instead, so people can't get the real path of the file. And it can also be before the public_html dir so no one can access without the script.
     
    nico_swd, Mar 11, 2008 IP
  10. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    centralexpert, Mar 12, 2008 IP