note 84381 deleted from function.readdir by peehaa
| From: | peehaa@php.net | Date: | Fri, 19 May 2017 12:55:01 +0000 |
| Subject: | note 84381 deleted from function.readdir by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-209069@lists.php.net to get a copy of this message | ||
Note Submitter: MetaNull
----
A simple directory browser... that handles the windows charset in filenames (it should work for
every iso-8859-1 characters).
<?php
$basepath = realpath("./pub/"); // Root directory
$path = realpath($basepath.$_GET["path"]); // Requested path
$relativepath = "./".substr_replace( $path, "", 0, strlen( $basepath ) );
if( "/" == substr( $relativepath, -1 )) { // Remove the trailing slash
$relativepath = substr( $relativepath, 0, -1 );
}
$dh = opendir( $path );
while( false !== ($file = readdir( $dh ))) {
if("." == $file) {continue;}
// converts the filename to utf8
$file_utf8 = iconv( "iso-8859-1", "utf-8", $file );
// encode the path ('path' part: already utf8; 'filename' part: still
iso-8859-1)
$link = str_replace( "%2F", "/", rawurlencode( "{$relativepath}/"
)) . rawurlencode( utf8_decode( "{$file_utf8}" ));
if( is_dir( "{$path}/{$file}" )) {
echo "<a
href=\"?path={$link}&\">{$file_utf8}</a><br/>"
} else {
echo "<a href=\"{$link}&\">{$file_utf8}</a><br/>"
}
}
}
?>