Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Directory Help!

Status
Not open for further replies.

deemarcus

Programmer
Aug 18, 2005
21
GB
Hi someone has given me the following code to display all files / folders in a given directory...

This works fine but how can i stop the user from going up to a directoy that is higher than a given path? i.e

the directory is...

home/htdocs/_files/_standard/_reports

want to stop the user from getting at

home/htdocs/_files/_standard

Here is the example..

<head>
</head>
<body>
<?php
// false - don't allow switching to the parent-directory of this script
// true - allow simple switching to the parent-directory (via 'href')

$allow_parent = "false";

//configuration ends here---------------------------------------------------------------------------------------------------------------------------

//=======================================================================================
$path=$_GET["path"];
$SCRIPT_NAME=getenv("SCRIPT_NAME");
//put directory content in arrays-----------------------------------------------------------------------------------------------------------------
//home/htdocs/_files/_standard/_reports
// if (!isset($path)) { $path = "./"; }
if (!isset($path)) { $path = "/home/htdocs/_files/_standard/_reports/"; }
if (!file_exists($path)) { echo "<h2>File not found!</h2>"; exit; }
if (strstr($path,"..")) { echo "<h2>invalid path!</h2>"; exit; }
$base_dir = getcwd();
chdir($path);
$current_dir = getcwd();
$directory = dir("./");
$directories_array = array();
$files_array = array();
while ($file = $directory->read()) {
if (is_dir($file) AND $file != ".") { $directories_array[] = $file; }
if (is_file($file)) { $files_array[] = $file; }
}
$directory->close();
//sort and output the arrays-----------------------------------------------------------------------------------------------------------------------
echo "<h2>Directory listing for ".basename($current_dir)."</h2>";
echo "<table>";
echo "<tr><th>Name</th><th>Size</th><th>Date</th></tr>";
sort($directories_array);
foreach($directories_array as $value) {
if ($value=="..") { $new_path=strrev(substr(strstr(substr(strstr(strrev($path),"/"),1),"/"),1)); }
else { $new_path=$path.$value; }
if (($value != "..") OR ($base_dir != $current_dir)) {
echo "<tr><td><a href=\"$SCRIPT_NAME?path=".urlencode($new_path."/")."\">$value</a><b>/</b></td><td></td><td>".gmdate("d M Y H:i",filemtime($value))."</td></tr>"; }
elseif ($allow_parent == "true") {
echo "<tr><td><a href=\"$value\">$value</a><b>/</b></td><td></td><td>".gmdate("d M Y H:i",filemtime($value))."</td></tr>"; }
}
sort($files_array);
foreach($files_array as $value) {
if($value != basename($SCRIPT_NAME) or $path!="./") {
$filesize=filesize($value);
if ($filesize > 1073741823) { $filesize = sprintf("%.1f",($filesize/1073741824))." GB"; }
elseif ($filesize > 1048575) { $filesize = sprintf("%.1f",($filesize/1048576))." MB"; }
elseif ($filesize > 1023) { $filesize = sprintf("%.1f",($filesize/1024))." kB"; }
else { $filesize = $filesize." byte"; }
echo "<tr><td><a href=\"$path$value\">$value</a></td><td>$filesize</td><td>".gmdate("d M Y H:i",filemtime($value))."</td></tr>";
}
}
echo "</table>";
?>
</body>
</html>

Please can anyone help!!!

Kindest thanks.
 
Thanks for replying.

Not sure how to do that (new to PHP!)

I think that this may have somthing to do with it but i cant see any difference if i change the value to "true" from "false"

$allow_parent = "false";

Regard

Dee
 
A standard dirlist:
Code:
<?php
$dir = "/home/user/public_html/folder/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           echo "filename: $file : filetype: " . filetype($dir . $file) . "<br />\n";
       }
       closedir($dh);
   }
}

change the code, so that the $dir is the base-dir..
eg. it can not go lower than the base-dir.

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks for the example, will play with adding it to my code. Will probably post back saying i screwed everything up!

Thanks,

Dee
 
Thanks, will post my working code (when it works)!!!!

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top