php – 如何在img / p /上删除Prestashop无用的图像

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何在img / p /上删除Prestashop无用的图像脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
PRestashop,当我删除一些产品时,图像保留在他们的目录/ img / p / so,此时我的图像目录接近2GB.
找到这个脚本来删除Prestashop上无用的和BBDD未链接的图像,但我不知道为什么它只检测孤立图像但不删除它们.也许在PHP配置上禁用unlink功能?可能吗?或者代码错了?还有另一种方法可以进行这种清理吗?

<?PHP
// root path of the shop
$shop_root='/home/myweb/public_htML/';
// limIT number of image files to check,set to 10 for testing
$limit=1000000000;


include $shop_root . '/config/settings.inc.PHP';
$pdo = new PDO( 'MysqL:host='._DB_SERVER_.';dbname='._DB_NAME_,_DB_USER_,_DB_passwd_ );
$pdo->setattribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$r=$pdo->query('select count(1) cnt From ps_image')->fetch();
echo 'count images database: '.$r['cnt'] . "<br />";

// reset some counters
$cnt_files=0;
$cnt_checked=0;
$cnt_not_found=0;
$cnt_found=0;

for($ii=1; ($ii<=9) && ($cnt_files != $limit); $ii++)
{
        $path=$shop_root.'img/p/'.$ii;
        delImage($path);
        for($jj=0; ($jj<=9) &amp;& ($cnt_files != $limit); $jj++)
        {       
                $path=$shop_root.'img/p/'.$ii.'/'.$jj;
                delImage($path);
                for($kk=0; ($kk<=9) && ($cnt_files != $limit); $kk++)
                {
                        $path=$shop_root.'img/p/'.$ii.'/'.$jj.'/'.$kk;
                        delImage($path);
                        for($ll=0; ($ll<=9) && ($cnt_files != $limit); $lL++)
                        {
                                $path=$shop_root.'img/p/'.$ii.'/'.$jj.'/'.$kk.'/'.$ll;
                                delImage($path);
                        }       
                }
        }

}
echo 'files: '.$cnt_files.' checked: '.$cnt_checked.' not_found: '.$cnt_not_found.' found: '.$cnt_found;

function delImage($imageDir)
{
        global $limit,$pdo,$cnt_files,$cnt_checked,$cnt_not_found,$cnt_found;
        if ($handle = @opendir($imageDir)) {                    //@ is wriiten to avoid warning message and is handled in else condition
                echo $imageDir."<BR />";
                while ($cnt_files != $limit && false !== ($entry = readdir($handle))) {
                        if ($entry != "." && $entry != "..") {
                                $cnt_files++;
                                $pi = explode('-',$entry);
                                if($pi[0]>0 && !empty($pi[1])) {
                                        $cnt_checked++;
                                        if(!checkExistsDb($pdo,$pi[0])) {
                                                $cnt_not_found++;
                                                echo 'rm '.$imageDir.'/'.$entry."<BR />";
                                                unlink($imageDir.'/'.$entry);
                                        } else {
                                                $cnt_found++;
                                        }
                                }
                        }
                }
                        closedir($handle);
  }
        else
        {
                echo $imageDir." doesn't exist".'<BR />';
        }

}

function checkExistsDb($pdo,$id_image) {
        $r=$pdo->query($q='select \'ok\' ok,(select id_image from ps_image where id_image = '.(int)$id_image.') id_image');
        $row=$r->fetch();
        if($row['ok']!='ok') die( 'Problem with query,please correct');
        return $row['id_image']?true:false;
}
?>

这是一个输出部分:

/home/myweb/public_html/img/p/9/9/7
/home/myweb/public_html/img/p/9/9/7/0 doesn't exist
/home/myweb/public_html/img/p/9/9/7/1 doesn't exist
/home/myweb/public_html/img/p/9/9/7/2 doesn't exist
/home/myweb/public_html/img/p/9/9/7/3 doesn't exist
/home/myweb/public_html/img/p/9/9/7/4 doesn't exist
/home/myweb/public_html/img/p/9/9/7/5 doesn't exist
/home/myweb/public_html/img/p/9/9/7/6 doesn't exist
/home/myweb/public_html/img/p/9/9/7/7 doesn't exist
/home/myweb/public_html/img/p/9/9/7/8 doesn't exist
/home/myweb/public_html/img/p/9/9/7/9 doesn't exist
/home/myweb/public_html/img/p/9/9/8
/home/myweb/public_html/img/p/9/9/8/0 doesn't exist
/home/myweb/public_html/img/p/9/9/8/1 doesn't exist
/home/myweb/public_html/img/p/9/9/8/2 doesn't exist
/home/myweb/public_html/img/p/9/9/8/3 doesn't exist
/home/myweb/public_html/img/p/9/9/8/4 doesn't exist
/home/myweb/public_html/img/p/9/9/8/5 doesn't exist
/home/myweb/public_html/img/p/9/9/8/6 doesn't exist
/home/myweb/public_html/img/p/9/9/8/7 doesn't exist
/home/myweb/public_html/img/p/9/9/8/8 doesn't exist
/home/myweb/public_html/img/p/9/9/8/9 doesn't exist
/home/myweb/public_html/img/p/9/9/9
/home/myweb/public_html/img/p/9/9/9/0 doesn't exist
/home/myweb/public_html/img/p/9/9/9/1 doesn't exist
/home/myweb/public_html/img/p/9/9/9/2 doesn't exist
/home/myweb/public_html/img/p/9/9/9/3 doesn't exist
/home/myweb/public_html/img/p/9/9/9/4 doesn't exist
/home/myweb/public_html/img/p/9/9/9/5 doesn't exist
/home/myweb/public_html/img/p/9/9/9/6 doesn't exist
/home/myweb/public_html/img/p/9/9/9/7 doesn't exist
/home/myweb/public_html/img/p/9/9/9/8 doesn't exist
/home/myweb/public_html/img/p/9/9/9/9 doesn't exist
files: 29013 checked: 22290 not_found: 0 found: 22290

解决方法

检查文件权限.如果PHP用户没有权限删除其他用户文件,则unlink功能将无效.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何在img / p /上删除Prestashop无用的图像全部内容,希望文章能够帮你解决php – 如何在img / p /上删除Prestashop无用的图像所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。