Hi all,

this is a tricky one.

The following script will display a folders contents into a table, and show the file name as a clickable link, allowing the user to download.

the problem is, when the script is ran, it displays the actual php, html files.

what im trying to do is to either point it at another directory (which im assuming is the line containing

$myDirectory = opendir(".");

but after a lot of combinations i cant seem to get it to work


Heres the whole script

<?php

// open this directory 
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory)) {
	$dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//	count elements in array
$indexCount	= count($dirArray);
Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
		print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
		print("<td>");
		print(filetype($dirArray[$index]));
		print("</td>");
		print("<td>");
		print(filesize($dirArray[$index]));
		print("</td>");
		print("</TR>\n");
	}
}
print("</TABLE>\n");

?>

Whitey, $myDirectory = opendir("."); is indeed the line that determines which directory is listed.

If you want to list some other directory then it depends on where it is relative to the php script location.

You need something like:

  • $myDirectory = opendir("./myDir"); (a dir within the current dir)
  • $myDirectory = opendir("./myDir1/myDir2"); (a dir within a dir within the current dir)
  • $myDirectory = opendir("../myDir"); (a dir one level up)
  • $myDirectory = opendir("../../myDir"); (a dir two levels up)
  • $myDirectory = opendir("../myDir1/myDir2"); (a dir up one level, then down into another)

It's actually very simple and I'm sure you can see the pattern.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.