Title: ptbm's Replies | WordPress.org

---

# ptbm

  [  ](https://wordpress.org/support/users/ptbm/)

 *   [Profile](https://wordpress.org/support/users/ptbm/)
 *   [Topics Started](https://wordpress.org/support/users/ptbm/topics/)
 *   [Replies Created](https://wordpress.org/support/users/ptbm/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/ptbm/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/ptbm/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/ptbm/engagements/)
 *   [Favorites](https://wordpress.org/support/users/ptbm/favorites/)

 Search replies:

## Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)

 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [How to Remove Unused Images](https://wordpress.org/support/topic/how-to-remove-unused-images/)
 *  Thread Starter [ptbm](https://wordpress.org/support/users/ptbm/)
 * (@ptbm)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/how-to-remove-unused-images/#post-2118045)
 * oh u mean re tweak the plugin? let me you if you can 🙂
 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [How to Remove Unused Images](https://wordpress.org/support/topic/how-to-remove-unused-images/)
 *  Thread Starter [ptbm](https://wordpress.org/support/users/ptbm/)
 * (@ptbm)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/how-to-remove-unused-images/#post-2118043)
 * qbproger, I have not found the solution.
 * All my image links are not broken so I dont think Broken Link Checker will work
   for my case.
 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [How to Remove Unused Images](https://wordpress.org/support/topic/how-to-remove-unused-images/)
 *  Thread Starter [ptbm](https://wordpress.org/support/users/ptbm/)
 * (@ptbm)
 * [15 years ago](https://wordpress.org/support/topic/how-to-remove-unused-images/#post-2117929)
 * no reply? 🙁
 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [How to Remove Unused Images](https://wordpress.org/support/topic/how-to-remove-unused-images/)
 *  Thread Starter [ptbm](https://wordpress.org/support/users/ptbm/)
 * (@ptbm)
 * [15 years ago](https://wordpress.org/support/topic/how-to-remove-unused-images/#post-2117657)
 * This is the DUI code:
 *     ```
       <?
       /*
       Plugin Name: DUI
       Version: 1.0
       Plugin URI: http://www.bobhobby.com/2008/02/24/delete-unused-image-files-plugin-for-wordpress/
       Author: Bob carret
       Author URI: http://www.bobhobby.com/
       Description: This plugin will delete UnUsed images file that was not referred by any post and page of wordpress
       */
   
       /*
       Copyright (c) 2008 www.bobhobby.com
       Released under the GPL license
       http://www.gnu.org/licenses/gpl.txt
   
       Disclaimer:
       	Use at your own risk. No warranty expressed or implied is provided.
       	This program is free software; you can redistribute it and/or modify
       	it under the terms of the GNU General Public License as published by
       	the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
        	See the GNU General Public License for more details.
   
           You should have received a copy of the GNU General Public License
           along with this program; if not, write to the Free Software
           Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   
       Requires : WordPress 2.3.X or newer ,PHP 5 +
   
       Usage :
       1. Download plugin and unzip
       2. Upload the plugin file to your WordPress plugins directory inside of wp-content.(i.e. www.YourDomain.com/blog/wp-content/plugin/dui/duixx.php)
       3. Activate it from the plugins menu inside of WordPress.
       4. Change Permissions monthly archives directory by Chmod to 777 with your favorite FTP Client Software(i.e. cuteFTP)
       5. Go to Admin Control panal -> Options -> DUI, select Unused images file that you want to delete then press delete button.
       6. This is IMPORTANT step after delete unused images file you must Chmod monthly archives directory to previous value(most are 755).
       */
   
       ### Load WP-Config File If This File Is Called Directly
       if (!function_exists('add_action')) {
       	require_once('../../../wp-config.php');
       }
   
       //  Check an Unused image file
       function CheckUnusedImage($ImageFileName) {
           global $wpdb,$table_prefix;
       	$result = intval($wpdb->get_var("SELECT COUNT(*) FROM ".$table_prefix."posts WHERE post_content LIKE '%/$ImageFileName%'"));
       	return $result>0;
       }
   
       // Get list image file
       function GetImageFileName($ImageDir)
       {
       	global $TotalImageCnt;
           $FileCnt = 0;
       	$UnUsedImages = array();
   
       	if (is_dir($ImageDir))
       	{  
   
                if ($DirHndl = opendir($ImageDir))
       		 {
                   $files = array();
                   while (($file = readdir($DirHndl)))
       			{
       			    $path =	pathinfo($file);
                       if (eregi('(jpg)|(gif)|(png)|(jpeg)', $path['extension']) || eregi('(thumbnail)',$path['basename'])) $files[] = $file;
                   }
                   closedir($DirHndl);
       			//print_r($files);
   
       			if (count($files))
       			{
       				foreach ($files as $fn)
       				{	 
   
       					if (!CheckUnusedImage($fn))
       					{
       						$home = get_option('home');
       						$upload_path = get_option('upload_path');
       						$UnUsedImages[$FileCnt] = $fn.'::'.$home.'/'.$upload_path.'/'.$ImageDir.'/'.$fn;
       						$FileCnt++;
       						$TotalImageCnt++;
       					}
       				}
       			}
               }
           }
         return $UnUsedImages;
       }
   
       // select directory
       function select_year_directory($year)
       {
           $Yeardirectory = array();
           $month = array('01','02','03','04','05','06','07','08','09','10','11','12');
           for ($i=0;$i<12;$i++)
       	{
   
       	   $dir =getcwd();
   
              if (is_dir($dir.'/'.$year.'/'.$month[$i]))
       	   {
       			 $Yeardirectory[$month[$i]] = GetImageFileName($year.'/'.$month[$i]);
       	   }
       	}
       	return $Yeardirectory;
       }
   
       // check image in directory
       function CheckInDir($Dir) {
   
       	if ($dh = opendir($Dir))
       	{
       		while ($el = readdir($dh)) {
       			$path = $el;
   
       			if (is_dir($path) && $el != '.' && $el != '..') {
       				$year_result[$path] = select_year_directory($path);
       				//echo $path.'<br/>';
       			}
       		}
       		closedir($dh);
       	}
       	return $year_result;
       }
       //-----------------------------------------------------------------------------------
       // add management in option menu
       function add_DUI_option_menu() {
       	if (function_exists('add_options_page')) { // is this check needed?
       		add_options_page('DUI option', 'DUI', 8, basename(__FILE__), 'DUI_options');
       	}
       }
   
       // main option -----------
       function DUI_options()
       {
       ?>
       <div class="wrap">
         	<h2>Delete Unused Images</h2>
   
       <?
       	if (array_key_exists('_submit_check', $_POST)) {
          		if (isset($_POST['im'])){
          			$im = $_POST['im'];
   
            		foreach($im as $DelectedImage) {
       		    	$fdt = explode(":",$DelectedImage);
       				chdir("../");
       				$upload_dir = get_option('upload_path');
       				chdir($upload_dir.'/'.$fdt[0]);
       				if (@unlink($fdt[1])) {
       					echo $fdt[0].'---> '.$fdt[1].' <font color="#FF0000">Deleted</font><br>';
       				}else{
       					echo $fdt[0].'---> '.$fdt[1].' <font color="#FF0000">Cannot delete file.Please change directory premissions on <font color="#000000">'.$fdt[0].'</font> to <font color="#000000">777</font></font><br>';
       				}
       				chdir("../../../");
       			}
   
       			echo "<hr>";
       		}
       	}
   
       	global $TotalImageCnt;
       	chdir("../");
       	$upload_dir = get_option('upload_path');
       	chdir($upload_dir);
       	$newdir = getcwd();
       	$TotalImageCnt=0;
       	$result = CheckInDir($newdir);
   
       	?>
   
       	<? echo "<b>There are total <font color='#FF0000'>".$TotalImageCnt."</font> unused images file</b><br><br>"; ?>
   
       	<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
       	<?
       	if (is_array($result)) {
           	foreach ($result as $year => $monthArr) {
       	    	foreach ($monthArr as $month => $UnUsedImagesArr) {
       		    	if (count($UnUsedImagesArr)<>0) {
       					echo '<b><font color="#007700">'.$year.'/'.$month.'</font></b><br>';
       					foreach ($UnUsedImagesArr as $UnUsedImages) {
       				    	$IMF = explode("::",$UnUsedImages);
       						echo '<input type="checkbox" name="im[]" value="'.$year.'/'.$month.':'.$IMF[0].'"/>'.$IMF[0].	'&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$IMF[1].'" target="_blank">View</a><br>';
       					}
       					echo '<hr>';
       				} // end if
       			}
       		}
       	}
       ?>
       <input type="hidden" name="_submit_check" value="1"/>
       <input type="submit" name="submit" class="button" value="<?php _e('Delete Checked Files'); ?>" />
       </form>
       <br />
       <br />
       <br />
       <a href="http://www.BobHobby.com" target="_blank">www.BobHobby.com</a>
       </div>
       <?
       }
   
       add_action('admin_menu', 'add_DUI_option_menu');
       ?>
       ```
   
 * Thank you
 *   Forum: [Installing WordPress](https://wordpress.org/support/forum/installation/)
   
   In reply to: [Cannot Import File](https://wordpress.org/support/topic/cannot-import-file/)
 *  Thread Starter [ptbm](https://wordpress.org/support/users/ptbm/)
 * (@ptbm)
 * [15 years ago](https://wordpress.org/support/topic/cannot-import-file/#post-2109693)
 * Nevermind.. got it worked out. got to clean browser’s cache.
 * Thank you

Viewing 5 replies - 1 through 5 (of 5 total)