this:
// copy image only if modified
$cid = explode('_', $cPath);
foreach($cid as $key => $value){
$cdat = tep_db_query("SELECT categories_name FROM categories_description WHERE categories_id = '" . $value . "' && language_id = '1'");
$cat = tep_db_fetch_array($cdat);
$categoryfile .= $cat[categories_name] . "/";
$_SESSION['categoryfile'] = $categoryfile;
}

$products_image = new upload('products_image');
$products_image->set_destination(DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile);
if ($products_image->parse() && $products_image->save()) {
$products_image_name = DIR_FS_PRODUCTS_IMAGES . $categoryfile . $products_image->filename;
} else {
-------------------------------------------------
replace with:

// copy image only if modified
$cid = explode('_', $cPath);
foreach($cid as $key => $value){
$cdat = tep_db_query("SELECT categories_name FROM categories_description WHERE categories_id = '" . $value . "' && language_id = '1'");
$cat = tep_db_fetch_array($cdat);if ($cat[categories_name] == !null){
$categoryfile .= $cat[categories_name] . "/";}
if (file_exists(DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile)==false)
{$str = DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile . $subcfile ;
mk_dir($str);
};
$_SESSION['categoryfile'] = $categoryfile;
}

$products_image = new upload('products_image');
$products_image->set_destination(DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile);
if ($products_image->parse() && $products_image->save()) {
$products_image_name = DIR_FS_PRODUCTS_IMAGES . $categoryfile . $products_image->filename;
} else {


/////////////////////////////////////////////////////////////
and this:
// check if the catalog image directory exists
if (is_dir(DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile . $subcfile)) {
if (!is_writeable(DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile . $subcfile)) $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE , 'error');
} else {
$messageStack->add(ERROR_CATALOG_PRODUCT_IMAGE_DIRECTORY_DOES_NO T_EXIST, 'error');
unset($_SESSION['categoryfile']);
}
------------------------------------------------
replace with:

if (is_dir(DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile . $subcfile)) {
if (!is_writeable(DIR_FS_CATALOG_IMAGES . DIR_FS_PRODUCTS_IMAGES . $categoryfile . $subcfile)) $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE , 'error');
} else {
$messageStack->add(ERROR_CATALOG_PRODUCT_IMAGE_DIRECTORY_DOES_NO T_EXIST, 'error');
}
function mk_dir($str, $mode=0777) {
if(!(is_dir($str) || @mkdir($str, $mode))) {
mk_dir(dirname($str));
mk_dir($str);
}
}
unset($_SESSION['categoryfile']);



////////////////////////////////////////////////////////
make these changes, then the categories directoires will be aoto build in products directories and products pics will be into their categoies directories. that's easy to manager all products pisc !
thanks,
if have any problem, content me with simomchen41@gmail.com

More...