我为自己找到了一个解决方案,我分享以供参考:
function microtime_float()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$start = microtime_float();
$b = '<br />'; $ad = 16; $cust = 139;
$directory = $_SERVER['DOCUMENT_ROOT'].'/ginew/advertiser/images/'. $cust .'/';
// echo $directory;
if( is_dir( $directory ) ) {
$handler = opendir( $directory );
$images = glob("$directory{*.jpg,*.JPG,*jpeg, *JPEG}", GLOB_BRACE);
$cnt = count($images);
echo 'total images in directory: '. $cnt.$b;
$i=0; $img1 = '';
while ($filename = readdir($handler)) {
if($filename != '.' && $filename != '..') {
$base = basename($filename, ".jpg"); // echo $base.$b;
$ext = substr(strrchr($filename, '.'), 0); // echo $ext.$b;
$imgsize = @filesize($directory.$filename); $imgsize = ($imgsize/1000000) .' mb';
if ( preg_match( '#'.$ad.'_([0-9]+)_'.$cust.'#', $base, $matches ) ) {
$i++;
$img = $i .' matches '. $matches[0] . $ext;
$img1.= $i .' matches '. $matches[1] .'<br>';
echo $img.$b;
} // END is_dir
echo $i .' --> '. $filename . $ext .' => '. $imgsize .'<br />';
} // END preg_match
} // END if filename
$end = microtime_float();
} // END while
echo '/ -------------------------- /<br>';
echo $img1.$b;
echo 'matches images found: '. $i . $b;
echo 'preg_match brauch : '.($end-$start).' Sekonnen';
输出:
目录中的总图像:6
1 匹配 16_2_139.jpg
2 匹配 16_7_139.jpg
3 匹配 16_1_139.jpg
/ -------------------------- /
1 匹配 2
2 匹配 7
3 匹配 1
matches images found: 3
preg_match brauch : 0.00037693977355957 Sekonnen
如果有人对此有更好的解决方案,请张贴感谢大家;-)