上传特定目录中的图像,如/i/m/image.jpg

I am looking to upload images on my server in the directory /media/catalog/product then it will take the first character of the image and place it in a directory which matches that character (also needs to create the directory if it doesnt exist) and then does the same for the second character.

e.g if I upload an image called product.jpg it will end up in the directory /media/catalog/product/p/r/

test.jpg -> /media/catalog/product/t/e/

the images are all in a folder on my computer, or I can upload them ALL to one directory.

I am trying to do this with Magento version 1.9

<?PHP

/* TRANSFORM FILES ARRAY */
function diverse_array($vector) { 
    $result = array(); 
    foreach($vector as $key1 => $value1) 
        foreach($value1 as $key2 => $value2) 
            $result[$key2][$key1] = $value2; 
    return $result; 
}

/* DO THE TRANSFORMATION, warning key for the superglobal $_FILES may be "picture" */
$upload = diverse_array($_FILES["upload"]);

/* create folder if not exists and then move file to the right folder */
foreach($upload as $current)
{
    mkdir("/media/catalog/product/" . $current["name"][0]);
    mkdir("/media/catalog/product/" . $current["name"][0] . "/" . $current["name"][1]);
    move_uploaded_file($current["tmp_name"],
    "media/catalog/product/" . $current["name"][0] . "/" . $current["name"][1] . "/" . $current["name"]);
}

note that you may want to change the rights for your directories see mkdir manual, and check the name of your input file.