The following code, how to change the sort by file name

Source: Internet
Author: User
Php

The following code, how to change the sort by file name. The following page list.php is can be listed in the C:\filelist file, you can need to sort by the file name, in addition, want to add an action called Open, is in the browser open file, not download. Thank you!


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)) < $basedir) {

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 < $basedir) $dir = $basedir;
?>



<title>File Listing</title>














File Listing

Sign Out






























































$dirs = @opendir ($dir); $count = 0; while ($file = @readdir ($dirs)) {$b = "$dir/$file"; $a = @is_dir ($b); if ($a = = "1") {if ($file! = "...") && $file! = ".") {if ($count < 1) {$count + +; echo " \ n "; echo " \ n "; echo " \ n "; } echo " \ n "; echo " \ n "; echo " \ n "; }}} @closedir ($dirs); ?>

$predir = Realpath ($dir. "/.. /");

if ($predir > $basedir) $predir = Str_replace ($basedir. "/", "", $predir);

if ($predir = = $basedir) $predir = ".";

if ($dir > $basedir) echo "Up level";

?>

Folder
$basedir? Str_replace ($basedir. "/", ", $dir. "/") : '') . UrlEncode ($file). " \ "> $file

























































$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 " \ n "; echo " \ n "; echo " \ n "; echo " \ n "; echo " \ n "; }} @closedir ($dirs); ?>
Filename Date Size Action
$file$lastsave$size KB$basedir? Str_replace ($basedir. "/", ", $dir. "/"): "). UrlEncode ($file)." \ ">download



Reply to discussion (solution)

Use the Glob function instead of the directory function. The result is a sort of
Of course you can also attach the Glob_nosort parameter to block this behavior

Where should I change? How to change it? Thank you!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.