RE: How can I add sub-directories in the images folder???

meltus <[email protected]> 7 Sep 2003 11:04:38 -0000
Newsgroups gmane.comp.web.oscommerce.features
Message-ID <75ef6f1485ffb417edcb2946aca6db77@osCommerce-Forums>
This message was sent from: Features
http://forums.oscommerce.com/viewtopic.php?p=225586#225586
----------------------------------------------------------------

i wrote a utility for doing this sometime ago

I know everyone tells you to do a database backup before trying something - but I can't stress enough to do it before running this code.  It will alter every product in your database.  Also keep a copy of your images somewhere else until you are happy it has done the correct thing.

It takes the current image location and moves it to its correct directory structire for categories and sub-categories and then rewirtes the database to reflect the new locations.

It will make the new directories automatically.

The way I tended to use it was to run it now and again when the images directory got out of hand - and it would clean everything up for you.

it does work - but run it at your own risk folks.

[code]<?php
/**
 * $Id: repair_thumbs.php,v 1.0 Exp $
 * [email protected]

 * osCommerce, Open Source E-Commerce Solutions
 *
 * http://www.oscommerce.com
 *
 * Copyright (c) 2003 osCommerce
 *
 * Released under the GNU General Public License
 */

require('includes/application_top.php');

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PRODUCT_INFO);

?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS;
?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET;
?>">

<title><?php echo TITLE ?></title>

<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">

<link rel="stylesheet" type="text/css" href="<?php echo THEMA_STYLE;
?>">
</head>
<pre>
<!-- header //-->

<?php require(DIR_WS_INCLUDES . 'header.php');
?>

<!-- header_eof //-->
<?php
function deldir($dir)
{
        $current_dir = opendir($dir);
        while ($entryname = readdir($current_dir)) {
                if (is_dir("$dir/$entryname") and ($entryname != "." and $entryname != "..")) {
                        deldir("${dir}/${entryname}");
                } elseif ($entryname != "." and $entryname != "..") {
                        unlink("${dir}/${entryname}");
                }
        }
        closedir($current_dir);
        rmdir(${dir});
}

function get_cats($categories_array = '', $parent_id = '0', $indent = '') {

    global $languages_id;
$parent_id = tep_db_prepare_input($parent_id);

    if (!is_array($categories_array)) $categories_array = array();


    $categories_query = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where parent_id = '" . tep_db_input($parent_id) . "' and c.categories_id = cd.categories_id and cd.language_id = '" . $languages_id . "' order by sort_order, cd.categories_name");
    while ($categories = tep_db_fetch_array($categories_query)) {

   $cat_string = str_replace("&", "", $categories['categories_name']);
   $cat_string = str_replace(" ", "", $cat_string);
   $cat_string = str_replace("nbsp", "", $cat_string);
   $cat_string = str_replace("/", "", $cat_string);
   $cat_string = str_replace(";", "", $cat_string);

echo $cat_string . "<br>";
      $categories_array[] = array('id' => $categories['categories_id'],
                                  'text' => $indent . $cat_string);


      if ($categories['categories_id'] != $parent_id) {        $categories_array = get_cats($categories_array, $categories['categories_id'], $indent . $cat_string . '/' );
      }
    }return $categories_array;}
$categories_array = get_cats($cPath, '', '');



$num_cats = count($categories_array);
//print_r($categories_array);

for ($i = 0; $i < $num_cats; $i++) {
        $cat_string = $categories_array[$i]['text'];

        echo $cat_string . "<BR>";
        // deldir(DIR_FS_CATALOG . DIR_WS_IMAGES . $cat_string);
        if (!file_exists(DIR_FS_CATALOG . DIR_WS_IMAGES . $cat_string)) {
                mkdir(DIR_FS_CATALOG . DIR_WS_IMAGES . $cat_string, 0777);
                }        $products_query = tep_db_query("select * from products_to_categories where categories_id = '" . $categories_array[$i]['id'] . "'");

        while ($products = tep_db_fetch_array($products_query)) {
                echo $products['products_id'] . ", ";
                $products_desc_query = tep_db_query("select products_image from products where products_id = '" . $products['products_id'] . "'");
                while ($products_desc = tep_db_fetch_array($products_desc_query)) {
                        $image = DIR_FS_CATALOG . DIR_WS_IMAGES . $products_desc['products_image'];
                        if (file_exists($image)) {
                                $image2 = explode('/', $image);
                                $new_image = DIR_FS_CATALOG . DIR_WS_IMAGES . $cat_string . '/' . $image2[count($image2)-1];

                                if ($new_image != $image) {
                                        if (!file_exists($new_image))
                                                copy($image, $new_image);                                        
										if (file_exists($new_image)) {                                                
											tep_db_query("update products set products_image = '" . $cat_string . '/' . $image2[count($image2)-1] . "' where products_id = '" . $products['products_id'] . "'");
                                        	unlink($image);
                                        }                                
								}
                        }
                }        
		}
}
?>
</pre>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php');?>[/code]