CodeIgniter Forums
Dynamically generating folders? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Dynamically generating folders? (/showthread.php?tid=9899)

Pages: 1 2


Dynamically generating folders? - El Forum - 07-11-2008

[eluser]markanderson993[/eluser]
Is there any way to dynamically generate a folder (named after the username) each time a user signs up (so that all his/her uploaded pictures can reside in that folder)? If this is not a very plausible solution, how then can I organize a server stuffed with profile images from various users? Thanks to anyone who takes the time to read this and respond Smile


Dynamically generating folders? - El Forum - 07-12-2008

[eluser]Seppo[/eluser]
You can create a folder from PHP, using mkdir


Dynamically generating folders? - El Forum - 07-12-2008

[eluser]Steve Grant[/eluser]
There are various ways of achieving what you're looking for.

Firstly, as already stated, there's PHP's mkdir function.

Secondly, you could maintain every file in the same directory and assign a unique and random filename to each one, mapping the generated filenames to the user id in the database.


Dynamically generating folders? - El Forum - 07-12-2008

[eluser]markanderson993[/eluser]
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?


Dynamically generating folders? - El Forum - 07-12-2008

[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.


Dynamically generating folders? - El Forum - 07-12-2008

[eluser]markanderson993[/eluser]
Thanks Steve for your informative response.


Dynamically generating folders? - El Forum - 07-12-2008

[eluser]markanderson993[/eluser]
One small question, I took your advice on randomizing folder names to heart. So I wrote a script to create a random folder name if the folder does not already exist. I am not receiving any errors but could you please glance over this to make sure it really is doing what it should be? Thanks a bunch

Code:
# Create a random directory name for user
            $this->ci->load->library('functions');
            
            $upload_folder = $this->ci->functions->randomise();
            
            $this->ci->db->select('upload_folder');
            $get_users_upload_folders = $this->ci->db->get('users');
            $upload_folders_array = $get_users_upload_folders->result();
            if (in_array($upload_folder, $upload_folders_array))
            {
                $redo_randomise = TRUE;
            } else {
                $redo_randomise = FALSE;
            }
            
            while ($redo_randomise)
            {
                $upload_folder = $this->ci->functions->randomise();
                $get_users_upload_folders = $this->ci->db->get('users');
                $upload_folders_array = $get_users_upload_folders->result();
                $this->ci->db->select('upload_folder');
                if (in_array($upload_folder, $upload_folders_array))
                {
                    $redo_randomise = TRUE;
                } else {
                    $redo_randomise = FALSE;
                }
            }
            
            mkdir('../uploads/'.$upload_folder.'/');



Dynamically generating folders? - El Forum - 07-12-2008

[eluser]marcoss[/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]

Use md5_file and you will get an unique file name, plus (and depending on your delete policies) you'll avoid duplicate in the case two or more users upload the same file.


Dynamically generating folders? - El Forum - 07-12-2008

[eluser]markanderson993[/eluser]
Grr! File uploading is now working, however, I am getting a "The temporary folder is missing" error. I've looked at the all the posts concerning this error but I am completely confused as to how to continue... help!

Is there a temporary directory, if so, where is it?
Is it even possible to utilize a temporary directory if each user has their own specific folder for their uploads?

A very confused... again,
- Mark



[EDIT] I've been doing a little research and I believe the solution is creating a tmp directory dictated by the php.ini settings.


Dynamically generating folders? - El Forum - 07-12-2008

[eluser]marcoss[/eluser]
That's right, the tmp folder is set in the php.ini, here http://us3.php.net/manual/en/ini.core.php#ini.upload-tmp-dir.