返回数据,遍历获取目录下的指定类型的文件。
1.
/* 返回数据 */
$result = json_encode(array(
“state” => “SUCCESS”,
“list” => $list,
“start” => $start,
“total” => count($files)
));
echo $result;
2.
/**
* 遍历获取目录下的指定类型的文件
* @param $path
* @param array $files
* @return array
*/
function getfiles($path, $allowFiles, $key, &$files = array()){
if (!is_dir($path)) return null;
if(substr($path, strlen($path) – 1) != ‘/’) $path .= ‘/’;
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != ‘.’ && $file != ‘..’) {
$path2 = $path . $file;
if (is_dir($path2)) {
getfiles($path2, $allowFiles, $key, $files);
} else {
if (preg_match(“/\.(“.$allowFiles.”)$/i”, $file) && preg_match(“/.*”. $key .”.*/i”, $file)) {
$files[] = array(
‘url’=> substr($path2, strlen($_SERVER[‘DOCUMENT_ROOT’])),
‘name’=> $file,
‘mtime’=> filemtime($path2)
);
}
}
}
}
return $files;
}
PHP遍历数组的方法汇总
<?php
$arr= array();
for($i= 0; $i< 50000; $i++){
$arr[]= $i*rand(1000,9999);
}
function GetRunTime()
{
list($usec,$sec)=explode(” “,microtime());
return ((float)$usec+(float)$sec);
}
######################################
$time_start= GetRunTime();
for($i= 0; $i< count($arr); $i++){
$str= $arr[$i];
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo ‘Used time of for:’.round($time_used, 7).'(s)<br /><br />’;
unset($str, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
while(list($key, $val)= each($arr)){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo ‘Used time of while:’.round($time_used, 7).'(s)<br /><br />’;
unset($str, $key, $val, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
foreach($arr as$key=> $val){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo ‘Used time of foreach:’.round($time_used, 7).'(s)<br /><br />’;
?>
<?php
$urls= array(‘aaa’,’bbb’,’ccc’,’ddd’);
foreach ($urls as $url){
echo “This Site url is $url! <br />”;
}
?>