Welcome Guest, Not a member yet? Register   Sign In
Dynamically generating folders?
#5

[eluser]Steve Grant[/eluser]
[quote author="pianoman993" date="1215892112"]Thanks for the all the useful input. Steve, I thought it was impossible to create a perfectly unique filename. If it is possible, how do I do it?[/quote]
I guess it would depend on how many files you believe may end up being put into the folder.

Firstly, you'll need to create an array of characters, featuring all characters that would fit into a valid filename. Decide how long you want the file name to be (32 characters excluding the file extension would probably suffice, giving you 86,662,973,277,706,162,286,946,811,886,609,896,461,828,096 permutations based on just the 26 letters in the standard Western alphabet and 0-9! ;-) ) and run some sort of randomiser such as:

Code:
function randomise()
{
    $activation_key = "";
    $random_string_length = 32;
    
    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');


    
    for($i = 0; $i < $random_string_length; $i++)
    {

        $random = mt_rand(0, count($chars) - 1);
        $activation_key .= $chars[$random];
    }
    
    return $activation_key;
}

Then before committing the filename to the database, you would probably want to run a check to ensure that the filename doesn't already exist (very unlikely given the number of permutations, but better to be safe than sorry!), obviously re-running the function above in the event of a match, and then use the file manipulation commands to place the file in the directory with the generated filename (plus file extension, of course). The filename should then be placed in the database.

If you're only allowing users to upload one image at a time, you can happily put the image filename as a column in your users table. If (as is most likely) you want to allow them to upload multiple images, you'd need a couple of extra tables. Firstly, an image table which would contain all the data about the image itself, i.e. image_id (auto_increment), filename, perhaps height/width attributes, size, etc. Secondly, a user_image table, which only needs to contain the primary key from the user table and the primary key from the image table. This provides the link.


Messages In This Thread
Dynamically generating folders? - by El Forum - 07-11-2008, 11:45 PM
Dynamically generating folders? - by El Forum - 07-12-2008, 12:18 AM
Dynamically generating folders? - by El Forum - 07-12-2008, 07:26 AM
Dynamically generating folders? - by El Forum - 07-12-2008, 08:48 AM
Dynamically generating folders? - by El Forum - 07-12-2008, 09:35 AM
Dynamically generating folders? - by El Forum - 07-12-2008, 10:50 AM
Dynamically generating folders? - by El Forum - 07-12-2008, 10:50 AM
Dynamically generating folders? - by El Forum - 07-12-2008, 01:26 PM
Dynamically generating folders? - by El Forum - 07-12-2008, 04:34 PM
Dynamically generating folders? - by El Forum - 07-12-2008, 07:09 PM
Dynamically generating folders? - by El Forum - 07-12-2008, 08:40 PM
Dynamically generating folders? - by El Forum - 07-12-2008, 08:58 PM
Dynamically generating folders? - by El Forum - 07-12-2008, 09:19 PM
Dynamically generating folders? - by El Forum - 07-12-2008, 10:47 PM
Dynamically generating folders? - by El Forum - 07-13-2008, 10:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB