[FIXED] Image and thumb stored in the same folder + additional thumb in resized
doorknob:
If anybody is interested, I wrote this and added it to the admin menu in the backend to remove unwanted image files
Code:
<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' )) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
* Removes image files in the product, category, vendor and merchant folders that are not referenced in the database
*/
mm_showMyFileName( __FILE__ );
$show_deletions = false;
if( $show_deletions ) {
echo "<b>Deleted Files</b><br />";
}
$count = 0;
// Create database object
$db = new ps_DB;
$allowed_files = array( 'index.html', 'index.htm' );
// Product full & thumbnail images
// Prepare an array of valid file names (thumbnails include sub-directory in file name)
$db->query( "SELECT product_full_image FROM #__{vm}_product UNION SELECT product_thumb_image FROM #__{vm}_product" );
$image_files = $db->loadResultArray();
// Get the list of current files in the main directory
$dir_name = JPATH_ROOT.DS.'components'.DS.'com_virtuemart'.DS.'shop_image'.DS.'product';
if( is_dir($dir_name) ) {
$file_names = scandir( $dir_name );
foreach( $file_names as $file_name ) {
$path_name = $dir_name.DS.$file_name;
if( is_file( $path_name ) && !in_array( strtolower( $file_name ), $allowed_files ) ) {
// Check whether the file is referenced in the product table
if( !in_array( $file_name, $image_files ) ) {
if( $show_deletions ) {
echo $path_name.'<br />';
}
unlink( $path_name );
$count++;
}
}
}
}
// Get the list of current files in the thumbnail sub- directory
$dir_name = $dir_name.DS.'resized';
if( is_dir($dir_name) ) {
$file_names = scandir( $dir_name );
foreach( $file_names as $file_name ) {
$path_name = $dir_name.DS.$file_name;
if( is_file( $path_name ) && !in_array( strtolower( $file_name ), $allowed_files ) ) {
// Check whether the file is referenced in the product table
// Note: '/' is used rather than DS because it must exactly match the value stored by VM
if( !in_array( 'resized/'.$file_name, $image_files ) ) {
if( $show_deletions ) {
echo $path_name.'<br />';
}
unlink( $path_name );
$count++;
}
}
}
}
// Category full & thumbnail images
// Prepare an array of valid file names (thumbnails include sub-directory in file name)
$db->query( "SELECT category_full_image FROM #__{vm}_category UNION SELECT category_thumb_image FROM #__{vm}_category" );
$image_files = $db->loadResultArray();
// Get the list of current files in the main directory
$dir_name = JPATH_ROOT.DS.'components'.DS.'com_virtuemart'.DS.'shop_image'.DS.'category';
if( is_dir($dir_name) ) {
$file_names = scandir( $dir_name );
foreach( $file_names as $file_name ) {
$path_name = $dir_name.DS.$file_name;
if( is_file( $path_name ) && !in_array( strtolower( $file_name ), $allowed_files ) ) {
// Check whether the file is referenced in the category table
if( !in_array( $file_name, $image_files ) ) {
if( $show_deletions ) {
echo $path_name.'<br />';
}
unlink( $path_name );
$count++;
}
}
}
}
// Get the list of current files in the thumbnail sub- directory
$dir_name = $dir_name.DS.'resized';
if( is_dir($dir_name) ) {
$file_names = scandir( $dir_name );
foreach( $file_names as $file_name ) {
$path_name = $dir_name.DS.$file_name;
if( is_file( $path_name ) && !in_array( strtolower( $file_name ), $allowed_files ) ) {
// Check whether the file is referenced in the category table
// Note: '/' is used rather than DS because it must exactly match the value stored by VM
if( !in_array( 'resized/'.$file_name, $image_files ) ) {
if( $show_deletions ) {
echo $path_name.'<br />';
}
unlink( $path_name );
$count++;
}
}
}
}
// Vendor full & thumbnail images
// Prepare an array of valid file names (vendor thumbnails in main directory)
$db->query( "SELECT vendor_full_image FROM #__{vm}_vendor UNION SELECT vendor_thumb_image FROM #__{vm}_vendor" );
$image_files = $db->loadResultArray();
// Get the list of current files in the main directory
$dir_name = JPATH_ROOT.DS.'components'.DS.'com_virtuemart'.DS.'shop_image'.DS.'vendor';
if( is_dir($dir_name) ) {
$file_names = scandir( $dir_name );
foreach( $file_names as $file_name ) {
$path_name = $dir_name.DS.$file_name;
if( is_file( $path_name ) && !in_array( strtolower( $file_name ), $allowed_files ) ) {
// Check whether the file is referenced in the vendor table
if( !in_array( $file_name, $image_files ) ) {
if( $show_deletions ) {
echo $path_name.'<br />';
}
unlink( $path_name );
$count++;
}
}
}
}
global $vmLogger;
$vmLogger->info( "Image File Check Completed - $count Files Deleted" );
// Create the object and print a form heading
$formObj = &new formFactory( 'Check Image Files' );
$formObj->startForm();
?>
<table class="adminform">
<tbody>
<tr>
<td>
<input type="submit" class="button" value="Click to Continue" style="width:100%;font-size:200%;" />
</td>
</tbody>
</table>
<?php
$modulename = 'store';
$pagename = 'index';
$option = 'com_virtuemart';
$formObj->finishForm( '', $modulename.'.'.$pagename, $option );
?>
Pixell:
Doorknob... Where do I put your code? Which file to modify.
This is very good to have in the backend menu.
doorknob:
This is a whole program.
I called it admin.check_vm_images.php and linked it to the vm admin menu by modifying header.php and adding the following code:
Code:
<li class="item-smenu vmicon vmicon-16-content">
<a href="<?php $sess->purl($_SERVER['PHP_SELF']."?pshop_mode=admin&page=admin.check_vm_images&module_id=".$module_id) ?>">Check Images & Thumbnails</a>
</li>
Phil
Pixell:
Thank you!
andrey_fix:
how to make virtuemart 1.0.15 ?
sorry for my english.
Navigation
[0] Message Index
[#] Next page
[*] Previous page