A
Anonymous
Guest
Hi,
I'm writing a PHP page that is supposed to read the contents of a folder and sort the contents into folders and files (I'm filtering out everything other than .htm, .html and .zip files.)
The code I have written works until it gets to a folder that contains no other folders in it then it lists the files under the folders section.
I'd be so grateful to anyone that can help
Here's the code. If you want to run it yourself simply change the value of $pagename to whatever you save the .php page as.
Thanks.
I'm writing a PHP page that is supposed to read the contents of a folder and sort the contents into folders and files (I'm filtering out everything other than .htm, .html and .zip files.)
The code I have written works until it gets to a folder that contains no other folders in it then it lists the files under the folders section.
I'd be so grateful to anyone that can help
Here's the code. If you want to run it yourself simply change the value of $pagename to whatever you save the .php page as.
Thanks.
Code:
<?php
$pagename = "page.php";
$extrapath = "";
$PARAM = $HTTP_GET_VARS;
foreach ( $PARAM as $key=>$value ) {
if ( $key == "extrapath" ) {
$extrapath = $value;
}
}
?>
<html>
<head>
<title><?php echo $extrapath ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$files = array();
$folders = array();
$documentroot = $_SERVER[ 'DOCUMENT_ROOT' ] . $extrapath;
$handler = opendir( $documentroot );
while ( $file = readdir( $handler ) ) {
if ( $file != '..' ) {
if( is_file( $file ) ) {
echo "file<br>";
$files[] = $file;
}
else {
echo "folder<br>";
$folders[] = $file;
}
}
}
closedir( $handler );
// output folders
print "<p>Folders:</p>";
foreach( $folders as $key=>$value ) {
if( $value == "." ) {
print '<a href="' . $pagename . '?extrapath=' . $value . '">Home</a><br>';
}
else {
print '<a href="' . $pagename . '?extrapath=' . $extrapath . '/' . $value . '">' . $value . '</a><br>';
}
}
// output files
print "<p>Files:</p>";
foreach( $files as $key=>$value ) {
$fileextension = strrchr( $value, "." );
if( $fileextension == ".htm" || $fileextension == ".html" ) {
print '<a href="' . $value . '">' . $value . '</a> (Viewable resource)<br>';
}
else if( $fileextension == ".zip" ) {
print '<a href="' . $value . '">' . $value . '</a> (Downloadable resource)<br>';
}
}
?>
</body>
</html>