PHP上传图片复制

I am using AgileUploader with PHP to allow users to upload images. My script checks if a folder exists, if not creates the folder based on the users session id. From there it allows user to upload a single file which duplicate this file to 3 different sizes.

Everything works great for 99.8% of the time however, twice now (out of thousands), I have had an upload folder replicate the root images folder. My folder structure is:

temp/images/sessionid/large
temp/images/sessionid/medium
temp/images/sessionid/thumbnails

The issue sometimes creates:

temp/images/sessionid/sessionid/sessionid/ALL OTHER FOLDERS IN temp/images/

I thought it may be a session issue but the folder is being created with a valid session id.

I am running 3 webservers all sync'ing to each other and the replication issue is causing some delay in valid images to be sync'ed because its replicating EVERY folder and file in the root temp/images.

I then thought it might be an issue with my resizer dropping the path:

$listing_image = glob($temp_main_folder . "/large/*.{jpg,gif,png}", GLOB_BRACE);
if(count($listing_image) < 7){
    if(move_uploaded_file($tmp_name, "$temp_main_folder/large/$name")){
        $resizeObj = new resize("$temp_main_folder/large/$name"); 
        $resizeObj -> resizeImage(350, 350, 'auto');
        $resizeObj -> saveImage("$temp_main_folder/medium/$name", 90);
        $resizeObj -> resizeImage(120, 120, 'auto');
        $resizeObj -> saveImage("$temp_main_folder/thumbnails/$name", 90);
    }
}

Anyway, like I said, it works 99.8% so I dont think its an issue with any of the code...but then again, it has failed twice.

My question is, is there a way for PHP to restrict the folder size when using mkdir or limiting the number of subfolders and file? Or any suggestions about some other validation techniques when it comes to uploading files.

Each folder will always have 3 subfolders, each subfolder will have a max of 6 files, each file will be a maximum of 1mb in size.