如何找出所有产品中的缺失图片?

昨天突然发现公司受理的一个网站有些图片不显示。初步认为是图片地址失效或者本地图片库图片缺失,于是写了个检查的小程序,写下来,面得忘记了。


<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require('includes/application_top.php');
$res = tep_db_query("select * from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and pd.products_id = p.products_id and pd.language_id = '" . (int) $languages_id . "'");
while ($product_info = tep_db_fetch_array($res)) {
    if (!empty($product_info['products_description'])) {
        if (preg_match('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $product_info['products_description'])) {

            echo '<span style="color:red;">' . $product_info['products_model'] . '</span>';
            echo '########## id:' . $product_info['products_id'];
            echo '----------------link:<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product_info['products_id']) . '">' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product_info['products_id']) . '</a>';
            echo '<br>';

            preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $product_info['products_description'], $outinfo);
            foreach ($outinfo[1] as $key => $out) {
                echo $out . '<br>';
                $hostnameby = substr($out, 0, 23);
                $hostnamebywww = substr($out, 0, 27);

                if ($hostnamebywww === 'http://xxx.xom/') {
                    $imgurl = substr($out, 27);
                }else if ($hostnameby === 'http://www.xxx.xom/') {
                    $imgurl = substr($out, 23);
                }else{
                    echo 'this is old<br>';
                    echo '<img src="'.$out.'" width="200" height="200"/><br>';
                }
                define('PC_PATH', dirname(__FILE__));
                define('DS', DIRECTORY_SEPARATOR);
                if (!file_exists(PC_PATH . DS . $imgurl)) {
                    echo '<img src="'.HTTP_SERVER . '/' . $imgurl . '" width="200" height="200"/><br>';
                    echo $imgurl.'<br>';
                }
                unset($imgurl);
            }
            echo '<br><br>';
        }
    }
}

require(DIR_WS_INCLUDES . 'application_bottom.php');
?>

首先用sql语句查找所要检查的产品

因为图片存在于产品描述中,我们只对有产品描述的产品进行查找

if (!empty($product_info['products_description'])) {}

产品描述中要有图片才可能出现不显示的情况,所以

if (preg_match('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $product_info['products_description'])) {}

正则表达式是我无耻的从网上拿来用的,鄙视一下。

之后输出一些关键信息查看

再用preg_match_all() 函数检查所以图片

foreach() 循环检查

输出没有的图片

if (!file_exists(PC_PATH . DS . $imgurl)) {
                    echo '<img src="'.HTTP_SERVER . '/' . $imgurl . '" width="200" height="200"/><br>';
                    echo $imgurl.'<br>';
                }

如果是外连图片就直接输出

}else{
                    echo 'this is old<br>';
                    echo '<img src="'.$out.'" width="200" height="200"/><br>';
                }

最后运行看输出的结果

唉,最后还得人工检查,真失败。如果能写成自动的就好了,目前没想到方法。


附:后来要求找出已经恢复的产品相的产品号

修改了下上面的程序,终于不用手工了

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//找出已更新的
require('includes/application_top.php');
$res = tep_db_query("select * from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and pd.products_id = p.products_id and pd.language_id = '" . (int) $languages_id . "'");
while ($product_info = tep_db_fetch_array($res)) {
    if (!empty($product_info['products_description'])) {
        if (preg_match('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $product_info['products_description'])) {

            echo '<span style="color:red;">' . $product_info['products_model'] . '</span>';
            echo '########## id:' . $product_info['products_id'];
            echo '----------------link:<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product_info['products_id']) . '">' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product_info['products_id']) . '</a>';
            echo '<br>';
            $link = tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product_info['products_id']);
            preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $product_info['products_description'], $outinfo);
            //print_r($outinfo);
            foreach ($outinfo[1] as $key => $out) {
                echo $out . '<br>';
                $hostnameby = substr($out, 0, 31);
                $hostnamebywww = substr($out, 0, 35);

                if ($hostnamebywww === 'http://www.xxx.xxx/picture/') {
                    $imgurl = substr($out, 35);
                    
                } else if ($hostnameby === 'http://xxx.xxx/picture/') {
                    $imgurl = substr($out, 31);
                    
                } else {
                    $imgurl = '';
                }
                if ($imgurl != '') {
                    echo '<img src="' . HTTP_SERVER . '/picture/' . $imgurl . '" width="200" height="200"/><br>';
                    echo $imgurl . '<br>';
                    
                    $content.=$product_info['products_model'];
                    $content.="," . $link;
                    $content.="," . HTTP_SERVER . '/picture/' . $imgurl;
                    //$content.=",";
                    $content = $content . "\r\n";
                }
                unset($imgurl);
            }
            echo '<br><br>';
        }
    }
}
$fp = fopen("imginfo.csv", 'w');
fwrite($fp, $content);
fclose($fp);

require(DIR_WS_INCLUDES . 'application_bottom.php');
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值