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