Molteri Posted September 18, 2005 Posted September 18, 2005 I have problem with imagenames . If i upload images (for different products) wich has same name they mix up with each other. So i need to rename images to unique name when uploaded. Here is the code where i need to add microtime() but i dont know how // copy image only if modified $products_image = new upload('products_image'); $products_image->set_destination(DIR_FS_CATALOG_IMAGES); if ($products_image->parse() && $products_image->save()) { $products_image_name = $products_image->filename; } else { $products_image_name = (isset($HTTP_POST_VARS['products_previous_image']) ? $HTTP_POST_VARS['products_previous_image'] : ''); } Thanks Molteri
Guest Posted September 18, 2005 Posted September 18, 2005 try this: // copy image only if modified $products_image = new upload('products_image'); $products_image->set_destination(DIR_FS_CATALOG_IMAGES); if ($products_image->parse() ) { $products_image->filename .= microtime(); if($products_image->save()) { $products_image_name = $products_image->filename; } } else { $products_image_name = (isset($HTTP_POST_VARS['products_previous_image']) ? $HTTP_POST_VARS['products_previous_image'] : ''); } backup your files first. And I am not sure if a file override could ever happen by the code like this.
Molteri Posted September 19, 2005 Author Posted September 19, 2005 Hi, Thanks alot it almost works. If i upload picture.jpg it names picture.jpg0.39977600 1127087973. But image doesnt show because time is after name. Thanks Molteri
Guest Posted September 19, 2005 Posted September 19, 2005 yes you right I completely forgot about the extension thing. I will probably correct it using explode() like this $new_name_array=explode(".",$products_image->filename ); $products_image->filename = $new_name_array[0] . microtime() . '.' . $new_name_array[1]; just replace with the above the old line I had and try it: $products_image->filename .= microtime(); I added the extra dot
Molteri Posted September 19, 2005 Author Posted September 19, 2005 HI! Thanks man I have worked with that myself almost two weeks. Im very pleased. :) Molteri
urbieta Posted March 2, 2006 Posted March 2, 2006 The morons that are updating shops are actually not renaiming image files from the cameras default file name so the rename image is very usefull in many situations. I had a hard time figuring out how to implement this awesome idea, but at last I ended up with this :) The file to edit is catalog/admin/categories.php // copy image only if modified $products_image = new upload('products_image'); $products_image->set_destination(DIR_FS_CATALOG_IMAGES); if ($products_image->parse() && $products_image->save()) { $products_image_name = $products_image->filename; } else { $products_image_name = (isset($HTTP_POST_VARS['products_previous_image']) ? $HTTP_POST_VARS['products_previous_image'] : ''); } break; } } will become this: // copy image only if modified $products_image = new upload('products_image'); $products_image->set_destination(DIR_FS_CATALOG_IMAGES); if ($products_image->parse() && $products_image->save()) { $theoldnameineed = $products_image->filename; $new_name_array=explode(".",$products_image->filename ); $products_image->filename = $new_name_array[0] . md5(microtime()) . '.' . $new_name_array[1]; rename(DIR_FS_CATALOG_IMAGES . $theoldnameineed, DIR_FS_CATALOG_IMAGES . $products_image->filename); $products_image_name = $products_image->filename; } else { $products_image_name = (isset($HTTP_POST_VARS['products_previous_image']) ? $HTTP_POST_VARS['products_previous_image'] : ''); } break; } } And thats it, in other words find $products_image_name = $products_image->filename; and replace with $theoldnameineed = $products_image->filename; $new_name_array=explode(".",$products_image->filename ); $products_image->filename = $new_name_array[0] . md5(microtime()) . '.' . $new_name_array[1]; rename(DIR_FS_CATALOG_IMAGES . $theoldnameineed, DIR_FS_CATALOG_IMAGES . $products_image->filename); $products_image_name = $products_image->filename;
Recommended Posts
Archived
This topic is now archived and is closed to further replies.