试试我为这样的任务写的这个函数。它被设计为与本地文件系统的工作,但它会与ftp://
网址的工作,只要你的系统有allow_url_fopen
启用(虽然DIR_SORT_ATIME
和DIR_SORT_CTIME
将无法正常工作):
// Constants to make usage more reader-friendly
define('DIR_SORT_NAME', 1);
define('DIR_SORT_SIZE', 2);
define('DIR_SORT_ATIME', 3);
define('DIR_SORT_MTIME', 4);
define('DIR_SORT_CTIME', 5);
function readdir_sorted_array ($dir, $sortCol = DIR_SORT_NAME, $sortDir = SORT_ASC) {
// Validate arguments
$dir = rtrim(str_replace('\\', '/', $dir), '/');
$sortCol = (int) ($sortCol >= 1 && $sortCol <= 5) ? $sortCol : 1;
$sortDir = ($sortDir == SORT_DESC) ? SORT_DESC : SORT_ASC;
$name = $size = $aTime = $mTime = $cTime = $table = array();
// Open the directory, return FALSE if we can't
if (!is_dir($dir) || (!$dp = opendir($dir))) return FALSE;
// Fetch a list of files in the directory and get stats
for ($i = 0; ($file = readdir($dp)) !== FALSE; $i++) {
if (!in_array($file, array('.','..'))) {
$path = "$dir/$file";
$row = array('name'=>$file,'size'=>filesize($path),'atime'=>fileatime($path),'mtime'=>filemtime($path),'ctime'=>filectime($path));
$name[$i] = $row['name'];
$size[$i] = $row['size'];
$aTime[$i] = $row['atime'];
$mTime[$i] = $row['mtime'];
$cTime[$i] = $row['ctime'];
$table[$i] = $row;
}
}
// Sort the results
switch ($sortCol) {
case DIR_SORT_NAME:
array_multisort($name, $sortDir, $table);
break;
case DIR_SORT_SIZE:
array_multisort($size, $sortDir, $name, SORT_ASC, $table);
break;
case DIR_SORT_ATIME:
array_multisort($aTime, $sortDir, $name, SORT_ASC, $table);
break;
case DIR_SORT_MTIME:
array_multisort($mTime, $sortDir, $name, SORT_ASC, $table);
break;
case DIR_SORT_CTIME:
array_multisort($cTime, $sortDir, $name, SORT_ASC, $table);
break;
}
// Return the result
return $table;
}
返回的一个排序的关联数组文件在给定的路径$dir
。
谢谢,我想,我会做与ftp_nlist($ conn,'-t。'); 但是是否有可能扭转这个顺序,目前这是最新的,最近我最先想要的。可以通过颠倒数组来完成,但也可以通过询问。我想我可以联合使用ftp_mdtm()。我只是想筛选最新的文件并对其进行处理, – Nitesh 2012-04-18 10:37:32
@Nitesh:您可以使用''-rt''来颠倒顺序。 – Jon 2012-04-18 10:53:21