php
如下的代码,如何改成按文件名排序。下面的页面 list.php是可以列出c:\filelist里的文件,可以需要用文件名来排序,另外,想多加一个动作叫Open,是在浏览器打开文件,不是下载。谢谢!
error_reporting(0);
$basedir = "c:/filelist";
if(!is_dir($basedir)) $basedir = dirname(__FILE__);
$file_name = $_GET['downfile'];
if(!empty($file_name)){
$file_dir = $basedir . "/" . $file_name;
if(dirname(realpath($file_dir))
echo "Permission Denied!";
exit;
}
if (!file_exists($file_dir)) {
echo "File Not Found!";
exit;
} else {
$file = fopen($file_dir,"r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_dir));
Header("Content-Disposition: attachment; filename=" . $file_name);
echo fread($file,filesize($file_dir));
fclose($file);
exit;
}
}
header("Content-type: text/html; charset=utf-8");
$requestDir = $_GET['dir'];
if(empty($requestDir)){
$dir = $basedir;
}else{
$dir = $basedir . "/" . $requestDir;
}
$dir = realpath($dir);
if($dir ?>
File Listing
$predir = realpath($dir . "/../"); if($predir > $basedir) $predir = str_replace($basedir . "/","",$predir); if($predir == $basedir) $predir = "."; if($dir > $basedir) echo "Up Level"; ?> |
$dirs=@opendir($dir);
$count = 0;
while($file=@readdir($dirs)) {
$b="$dir/$file";
$a=@is_dir($b);
if($a=="1"){
if($file!=".."&&$file!=".") {
if($count
$count ++;
echo "\n";
echo "Folder | \n";
echo "
\n";
}
echo "\n";
echo " $basedir ? str_replace($basedir . "/",'',$dir . "/") : '') . urlencode($file)."\">$file | \n";
echo "
\n";
}
}
}
@closedir($dirs);
?>
Filename |
Date |
Size |
Action |
$dirs=@opendir($dir);
while ($file=@readdir($dirs)) {
$b="$dir/$file";
$a=@is_dir($b);
if($a=="0"){
$size=@filesize("$dir/$file");
$size=$size/1024 ;
$size= @number_format($size,2);
$lastsave=@date("Y-n-d H:i:s",filemtime("$dir/$file"));
echo "\n";
echo "$file | \n";
echo "$lastsave | \n";
echo "$size KB | \n";
echo " $basedir ? str_replace($basedir . "/",'',$dir . "/") : '').urlencode($file)."\">Download | \n";
echo "
\n";
}
}
@closedir($dirs);
?>
回复讨论(解决方案)
不用目录函数,改用 glob 函数。结果就是排序的
当然你也可以附加 GLOB_NOSORT 参数阻止这一行为
请问该改哪里? 如何改呢? 谢谢!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn