Filemanaging for Download

Discussion in 'PHP' started by Kalyse, Mar 10, 2007.

  1. #1
    Hello,

    I am about to finish off my CMS for my site and Im just on the file downloading area.

    I am not entirely sure how I should code this. I have been thinking and would like some input.

    I am aiming for something a bit like:

    http://halflife2.filefront.com/file/HalfLife_2_Garrys_Mod;50929

    For example.

    Anyway, My question is: How should I save the files?

    Can I save the files, (which will probably reach the 10k mark and in excess, lots of files/huge file system) in a single directory.

    So call the directory /files for example.

    I will be able to upload any file I want, lets say I upload a file called:

    'myfile.zip'

    The script I was thinking will automatically change the file name to an ID, the ID will be incremental, so since this is the first ID, I will rename the file to 1.zip (I supose its okay to keep the file extension? Im not sure if that should be changed to?)

    Then it enters the actual file name into a database table.

    When someone then tries to download the file through the CMS using:

    site.com/files/myfile.zip

    It uses rewrite to call the CMS script, lets call it script.php

    Then it looks up the filename in the database. So myfile.zip so see what the ID is.

    I then use... (I dont remember the means to do it, but I then send it to the user?) Infact how would I do this bit? And also send it so that the file is not called 1.zip but myfile.zip

    Is what I have described possible and what functions for the actual file download would I need?

    Basically /files/myfile.zip is a script and not the actual file :)
     
    Kalyse, Mar 10, 2007 IP
  2. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would save the files as IDs, no extension.

    Don't forget to add an htaccess file with "deny from all" to keep people from downloading files they don't have access to.

    $downloadAs = "myfile.zip";
    $fileSource = "1";
    
    if (file_exists("files/$fileSource")) {
    
       header('Content-type: application/zip');
       header('Content-Disposition: attachment; filename="' . $downloadAs . '"');
    
       readfile($fileSource);
    
    }
    PHP:
     
    Robert Plank, Mar 10, 2007 IP
  3. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay,

    So is my method actually a 'good method'

    Like I said I have never done this before so I just pseudo coded it all in my head and I believed using IDs for the files was the best way :)

    Thanks.
     
    Kalyse, Mar 10, 2007 IP
  4. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It is good as long as you do not link directly to the files (run it through the script). The other ways I can think of doing it would be:

    1. Generate an MD5 hash from the file contents and save as the file name, and store that file name in the database. (OK but too complicated... the ID thing will work fine)

    2. Store the contents of the whole file in the database (BAD - especially since the file could be 50 MB or more)
     
    Robert Plank, Mar 10, 2007 IP
  5. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #5

    I was going to do this for my screenshots area.


    Do you think its okay on images.

    Maximum 1mb in size?

    What sort of load am I talking about extra?
     
    Kalyse, Mar 10, 2007 IP
  6. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you think it will reach the 10k mark... 10k * 1 mb = 10 gb... too big. Saving it as a regular file would still be better.
     
    Robert Plank, Mar 10, 2007 IP