Upload image evenly into a directory structure
<p>Im sure this question has been asked thousand of times, so here goes my version...</p>

<p>I have a form that uploads images...
Every image contains an unique id. I use the following function to generate my unid id:</p>

<pre><code>function generateUnid($key) {   
    $name = $_FILES[$key]['name']; //get image name from global variable $_FILES
    $ext = pathinfo($name, PATHINFO_EXTENSION); //get image extension
    $prefix = 'fc'; //prefix for unid

    do {
       $unid = uniqid($prefix, true); //generate a unid
       $filename = $unid . '.' . $ext; //replace image name with unid
       $path = PATH_UPLOAD_ARTWORK . $filename; // image path
    } while (file_exists($path)); // check if the image name exists

    return $filename;
}
</code></pre>

<p>A sample of return values is:</p>

<pre><code>fc4e7801523a04e6.06876802.jpg
</code></pre>

<p>So far so good. Now, i want to create some sort of directory structure for my images. Something similar like:</p>

<pre><code>0
   0
   1
   2
      fc4e7801523a04e6.06876802.jpg
      ...
   3
   ...
1
  0
  1
  2
  3
  ...
2
  0
  1
  ...
</code></pre>

<p>I could probably get the last 2 integers in my unique id for filing the image in the correct directory. But, i'm not to sure if that is the correct strategy...
How can i make sure that the images are filed evenly in the folders. I don't want to find my self with one folder that contains 12 000 images and one folder with 1 500 images...</p>

<p>Am i doing it the correct way by extracting the last 2 numbers of my uniq? Are there better ways for filing the image evenly?</p>

<p>Thanks</p>
