Creating a download script

Discussion in 'PHP' started by Isaac, Jun 29, 2008.

  1. #1
    Hey everyone,

    I'm wondering how I would create a download script where all I have to do is upload a file to a folder, and it automatically shows up for download on the download page. This is what www.dohgames.com has, and it would really be handy for the website I am developing which will have tons of files for download that I don't want to enter in manually for it to be downloadable.

    If anyone could explain or direct me to a tutorial, that would be great.

    Thanks
     
    Isaac, Jun 29, 2008 IP
  2. foreal

    foreal Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    While uploading put something(filename,patch, etc.) to database.
    And for download page just extract that data from database.
     
    foreal, Jun 29, 2008 IP
  3. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok, can you explain how to set it up so when I upload a file, it automatically adds that info to the database?
     
    Isaac, Jun 29, 2008 IP
  4. foreal

    foreal Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
  5. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, I did a bit of reading there, and I'm lost. Would I do that, and if it returns true, then I would have it insert ['userfile'] or ['name'] into the database? But isn't that only for if you upload from a page I set up on the site? I'm wanting to simply upload the files via an FTP client like FlashFXP and have the files show up for download on a page. Is that possible?
     
    Isaac, Jun 29, 2008 IP
  6. foreal

    foreal Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, I thought that you want to upload files via form on webpage.

    About uploading through FTP.
    Put the files in a seperate directory and check all the files in directory. Then just make links to them...
    http://php.net/manual/en/function.readdir.php
     
    foreal, Jun 29, 2008 IP
    Isaac likes this.
  7. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just what I was looking for, thanks.
     
    Isaac, Jun 29, 2008 IP
  8. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hey I've got it working, but now I have another question. I have my files for download in a directory above /public_html/. How do I link to the files so they can be downloaded?
     
    Isaac, Jun 29, 2008 IP
  9. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #9
    you have to make PHP put the file name and the file size in the headers then print the file. I will give you the code when i get home if no one else has posted it
     
    King Goilio, Jun 29, 2008 IP
  10. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Here's the code I'm using:
    
    <?php
    $count = 0;
    if ($handle = opendir('./../downloads/nes')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {$count++;
                print("<a href=\"".$file."\">".$file."</a><br />\n");
            }
        }
    echo '<br /><br /><a href="..">Return</a>';
        closedir($handle);
    }
    ?>
    
    
    Code (markup):
     
    Isaac, Jun 29, 2008 IP
  11. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #11
    So what I'm wondering is, what do I change
    print("<a href=\"".$file."\">".$file."</a><br />\n");
    
    Code (markup):
    to so that the link can be to a file in a directory above /public_html/ ?
     
    Isaac, Jun 30, 2008 IP
  12. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #12
    you cant the way webservers work it you can only access the web directory
    alltho you could use PHP and .htaccess to do it
     
    King Goilio, Jun 30, 2008 IP
  13. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I'd like to know how to do it with PHP and .htaccess. Can you explain or direct me to a tutorial?
     
    Isaac, Jul 1, 2008 IP
  14. Isaac

    Isaac Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Nevermind that. Using the code I posted above, how would I make it show the links in alphabetical order?
     
    Isaac, Jul 2, 2008 IP