## PHP

Moderators: None (Apply to moderate this forum)
Number of posts: 5013

This Forum Only

sort directory / file listing Posted by codecraig on 9 Jan 2004 at 8:55 AM
i have a script which displays a list of directories, and if u click on the link, it shows the files inside of it. However, it does not display them alphabetically as shown in windows, etc.

here is my code:
function showIt($path) {$dir = opendir($path);$x=0;
while ($file = readdir($dir))
{
//if(strchr($file,".")==".gif") //check that it's really a gif image if($x > 1) {
$fullPath =$path . $file; if(is_dir($fullPath)) {
print("<b>file</b><BR><hr width=40% align=left>"); print(" " . showIt(fullPath) . "<BR>");
}
else {
$pieces = explode(".",$file);

$pathToFile =$path ."/". $file; if(is_dir($pathToFile)) {
print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"tree\folder.gif\">&nbsp;&nbsp;";
print "<a href=\"showAll.php?path=$pathToFile\">$file</a><BR>";
}
else {
print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"tree\\text.gif\">&nbsp;&nbsp;";
print "<a href=\"$pathToFile\">$file</a><BR>";
}

}
}
$x++; } closedir($dir);
}


any ideas how i can get it sorted?? no database available..and i am trying not to use a text file,etc..i want it to be dynamic.

thanks,
Craig
Code:Craig
Re: sort directory / file listing Posted by Jonathan on 9 Jan 2004 at 9:02 AM
Hi,

I think your best bet is to read all the filenames into an array, then sort that array and then loop through that.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Re: sort directory / file listing Posted by DarQ on 9 Jan 2004 at 6:18 PM
: Hi,
:
: I think your best bet is to read all the filenames into an array, then sort that array and then loop through that.

yes, thats the only way. use sort or asort (for associative arrays). and again, php.net might come in handy, there are some more sort functions.

: Jonathan
:
: ###
: for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
:

DarQ
NEW url--> http://mark.space.servehttp.com

Re: sort directory / file listing Posted by codecraig on 9 Jan 2004 at 8:09 PM
means i will have to rewrite my code, since i dont use arrays..i just print the directories as i find them.

damn!
Code:Craig

Re: sort directory / file listing Posted by DarQ on 10 Jan 2004 at 3:02 AM
: means i will have to rewrite my code, since i dont use arrays..i just print the directories as i find them.

come on, its not so hard. instead of printing them directly, put them into an array. then sort and loop through the array. looks to me its just a matter of 5 additional lines of code.

you can do it

:
: damn!
: Code:Craig
:
:

DarQ
NEW url--> http://mark.space.servehttp.com

Re: sort directory / file listing Posted by codecraig on 13 Jan 2004 at 6:03 AM
: : means i will have to rewrite my code, since i dont use arrays..i just print the directories as i find them.
:
:
: come on, its not so hard. instead of printing them directly, put them into an array. then sort and loop through the array. looks to me its just a matter of 5 additional lines of code.
:
: you can do it
:

: :
: : damn!
: : Code:Craig
: :
: :
:
: DarQ
: NEW url--> http://mark.space.servehttp.com
:
:

ok, ok it's done! Thanks for the inspiration to just do it. I have the script all coded out and i have little icons (to show a folder, file, etc)...not to bad! Anyways, I will post a link to the script once I make it secure. I can't just post the script on my site or someone could see all my directories if they wanted! haha...

peace

Code:Craig

Re: sort directory / file listing Posted by Jonathan on 13 Jan 2004 at 6:20 AM
: : : means i will have to rewrite my code, since i dont use arrays..i just print the directories as i find them.
: :
: :
: : come on, its not so hard. instead of printing them directly, put them into an array. then sort and loop through the array. looks to me its just a matter of 5 additional lines of code.
: :
: : you can do it
: :

: : :
: : : damn!
: : : Code:Craig
: : :
: : :
: :
: : DarQ
: : NEW url--> http://mark.space.servehttp.com
: :
: :
:
: ok, ok it's done! Thanks for the inspiration to just do it. I have the script all coded out and i have little icons (to show a folder, file, etc)...not to bad! Anyways, I will post a link to the script once I make it secure. I can't just post the script on my site or someone could see all my directories if they wanted! haha...
:
: peace
Am I allowed to do "security testing" on it?

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Re: sort directory / file listing Posted by DarQ on 13 Jan 2004 at 3:33 PM

I'd trust Jonathan.

: Am I allowed to do "security testing" on it?
:
: Jonathan
:
: ###
: for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
:

DarQ
NEW url--> http://mark.space.servehttp.com

the CODE Posted by codecraig on 14 Jan 2004 at 4:26 AM
This message was edited by codecraig at 2004-1-14 4:26:42

here's the code, you can try it yourself

www.codecraig.com/images/text.gif
www.codecraig.com/images/textfolder.gif
www.codecraig.com/images/folder.gif

<?php

/**
*	Author: Craig Wickesser <craig@codecraig.com>
*	Date: Jan. 10, 2004
*
*	Prerequisites:
*		1. PHP 4.3+
*		2. directory named images with the following images:
*				text.gif
*				folder.gif
*				textfolder.gif
*		3. The supplied path must be a directory that is a in the same
*		   directory as this script.
*		   Example:
*			  /www/testDir
*			  /www/showDirsAndFiles.php
*			  path = testDir/
*
*		Your final directory structure should be:
*			/your_path/showDirsAndFiles.php
*			/your_path/images/
*				/your_path/images/text.gif
*				/your_path/images/folder.gif
*				/your_path/images/textfolder.gif
*
*	OPTIONS:
*		Turn off details:  In the showIt() method change the
*							$showDetails variable to false **/ if((isset($_GET[path]))&&($_GET[path] != "")) { // Get the path and display folders and files$path = $_GET[path]; // to stop people from looking at to much :) (not the best method, but good 4 now) if(($path == ".")||($path == "..")||($path == "./")||($path == "/..")||($path == "./..")||($path == "../")||($path == "../../")||($path == "../..")||($path == "/")||($path == "")) showError(); else {$error = false;
$pathPieces = explode("/",$_SERVER['SCRIPT_NAME']);
$images = "/" .$pathPieces[1] . "/" . $images; if(count(explode("/",$path)) == 1)
$path =$path . "/";
showIt($path); } } else { // No path was given. Display error message and form to get path. showError(); } /** * Displays folders and files given a path. * @param$path
**/
function showIt($path) { // initialize variables$directories = array();
$files = array();$dirPath = array();
$filePath = array();$count = -1;
$fileAndDirCounter = 0;$showDetails = true;

// open directory handle
if(is_dir($path)) { if($dir = opendir($path)) {$pathPieces = explode("/", $path); // Print heading // If path is just <directory_name>/ if($pathPieces[1] == "")
{
print "<img src=\"images\\textfolder.gif\">&nbsp;&nbsp;";
print("<B>pathPieces[0]</b><hr width=40% align=left>"); } else { foreach(pathPieces as $v) {$count++;
}
$count--; print "<img src=\"images\\textfolder.gif\">&nbsp;&nbsp;"; print("<B>$pathPieces[count]</b><hr width=40% align=left>"); } // spin through files/directories indir
while ($file = readdir($dir))
{
// first two files/directories are always . and .. so don't count them
if($fileAndDirCounter > 1) { // this should be <directory_name>/someFile.txt // OR <directory_name>/<directory_name>$fullPath = $path .$file;

if(is_dir($fullPath)) { // Keep track of the directories and their paths$dirPath[$fileAndDirCounter] =$fullPath . "/";
$directories[$fileAndDirCounter] = $file; } else { // Keep track of the files and their paths$filePath[$fileAndDirCounter] =$fullPath;
$files[$fileAndDirCounter] = $file; } // end else } // end if$fileAndDirCounter++;
} // end while loop

// Close handle to directory
closedir($dir); // 1. Sort the array of directories // 2. Print the directories with folder icons print "<table>"; if(count($directories) != 0) {
natcasesort($directories); reset($directories);
$counter = count($directories) + 2;
$tempCounter = 2;$newArray = array_merge($dirPath,$directories);
$half = count($newArray) / 2;
$y = 0; for($x=0; $x <$half ;$x++) {$t = $half +$y;
print "<tr>";
print "<td>";
print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images\folder.gif\">&nbsp;&nbsp;";
print "<a href=\"showDirsAndFiles.php?path=$newArray[$x]\">$newArray[$t]</a>";
print "</td>";
print "</tr>";
$y++; } } print "</table>"; // 1. Sort the array of files // 2. Print the files with text icons print "<table>"; if(count($files) != 0) {
natcasesort($files); reset($files);
$counter = count($files) + 2;
$tempCounter = 2;$newArray = array_merge($filePath,$files);
$half = count($newArray) / 2;
$y = 0; for($x=0; $x <$half ;$x++) {$t = $half +$y;
print "<tr>";
print "<td>";
print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=images\\text.gif>&nbsp;&nbsp;";
print "<a href=\"$newArray[$x]\">$newArray[$t]</a>";
print "</td>";
if(showDetails) { print "<td align=left>"; print "<font size=2>"; print "&nbsp;&nbsp;&nbsp;"; print "Size: " . fsize(newArray[$x]); print "</font>"; print "</td>"; print "<td>"; print "&nbsp;&nbsp;&nbsp;"; print "<font size=2>"; print "Modified: " . date ("n/j/Y g:i a", filemtime($newArray[$x])) . ""; print "</font>"; print "</td>"; } print "</tr>";$y++;
}
}
print "</table>";
}
else
{
print "<font color=red><B>Error</B></font>";
print "&nbsp;&nbsp;&nbsp;";
print "Could not open directory handle to <b>$path</b>"; print "";$error = true;
}
}
else
{
print "<font color=red><B>Error</B></font>";
print "&nbsp;&nbsp;&nbsp;";
print "You entered an invalid directory: <b>$path</b>"; print "";$error = true;
}
}

if(!$error) { print ""; print "<a href=\"javascript:history.go(-1)\">Go Back</a>"; } // This function gets the file size and displays label function fsize($file) {
$a = array("B", "KB", "MB", "GB", "TB", "PB");$pos = 0;
$size = filesize($file);
while ($size >= 1024) {$size /= 1024;
$pos++; } return round($size,2)." ".$a[$pos];
}

function showError() {
// No path was given.  Display error message and form to get path.
print "<font color=red><B>Error</B></font>";
print "&nbsp;&nbsp;&nbsp;";
print "No path was given.  Please provide a path in the following format: &lt;directory_name>/";
print "";
print "&nbsp;&nbsp;&nbsp;";
print "<B>Example:</B> test/";
print "";
print "<form name=frmNeedPath action=showDirsAndFiles.php method=get>";
print "Path: ";
print "<input type=text length=15 name=path>";
print "\t";
print "<input type=submit value=submit>";
print "</form>";
$error = true; } ?>  Code:Craig Re: the CODE Posted by DarQ on 14 Jan 2004 at 4:37 AM This message was edited by Moderator at 2004-1-14 4:38:19 the code looks decent. nicely indented. though i'd use a little bit more empty lines. ah well, the readablility of your code is allright anyway ;) DarQ NEW url--> http://mark.space.servehttp.com Re: the CODE Posted by codecraig on 14 Jan 2004 at 6:04 AM : This message was edited by Moderator at 2004-1-14 4:38:19 : the code looks decent. nicely indented. though i'd use a little bit more empty lines. ah well, the readablility of your code is allright anyway ;) : : DarQ : NEW url--> http://mark.space.servehttp.com : : : : yes, perhaps some more blank lines. However, the indentation should be good...i did it in dreamweaver so i could have some syntax highlighting, and help with the indenting! peace, Code:Craig Re: the CODE Posted by Jonathan on 14 Jan 2004 at 10:42 AM I've had a VERY evil physics exam today, I'm going for dinner now and I'm probably out a bit tonight. I have exploited your code, it does have a security hole and I'll explain later. You can without too much difficulty get at directories below the ones you want to offer people. I'll try and give pointers on how to tighten it up too. Jonathan ### for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&& (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X; /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b$::c hack$1."); Re: the CODE Posted by Jonathan on 14 Jan 2004 at 4:49 PM I installed it on my own personal site, http://www.jwcs.net/~jonathan/. It contains a folder called amamp, which I could look at fine. I then did:- http://www.jwcs.net/~jonathan/test.php?path=amamp/../../../ And in no time, was heading way back down the directory tree below the script. I couldn't go much further than that because my server has PHP configured to start doing its own limiting, but I got to the root of the site and could have looked around things I probably should not. My question to you would be, should the user *ever* be able to use a ..? I'd suggest not. So just use strpos to see if$path contains a .. and if it does show an error. Also check that the first character of $path isn't a /. That way, you have blocked the two obvious ways of wandering a little further down the directory tree than may be liked. Have fun, Jonathan ### for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&& (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X; /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b$::c hack$1."); Re: the CODE Posted by codecraig on 16 Jan 2004 at 10:23 AM yes your right. i noticed that also, and the version that is running on my site disables those abilities (../, /, ./, /.., etc)...i did that after posting it on here. however, I did it the long way of checking: if(($path == ".")||(\$path == "../"))

etc..

strpos should be much easier and better.

Thanks,

Code:Craig

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic