Log in

View Full Version : Listing file in a directory problem



arsenalbates
02-06-2011, 07:41 PM
Im having some trouble with some code that lists the content of a directory. at the moment it is supose to list the name of the file, the type and the file size. the code is displaying the file name fine but not the File Type or File Size. im getting error "Warning: filetype() [function.filetype]: Lstat failed for Signiture.jpg in C:\xampp\htdocs\Login\dean\index.php on line 62"

it works if i list the files in the same directory as this php code but not if i list files in a sub directory e.g $username/Uploads

Here is the code im using

<?php
// open this directory

$myDirectory = opendir("./Uploads/");

// 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=0 cellpadding=5 cellspacing=15 class=whitelinks>\n");
print("<TR><TH>File Name</TH><th>File Type</th><th>File Size</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=\"./Uploads/$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");

?>

arsenalbates
02-13-2011, 09:12 PM
Anyone got any idea on what the problem may be ? i still have no luck

bluewalrus
02-13-2011, 09:28 PM
You need to include the location of the file in filetype and filesize.




<?php
// open this directory
$directory = "./Uploads/";
$myDirectory = opendir($directory);

// 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
?>
<table border="0" cellpadding="5" cellspacing="15" class="whitelinks">
<tr>
<th>File Name</th>
<th>File Type</th>
<th>File Size</th>
</tr>
<?php
// 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
?>
<tr>
<td><a href="<?php echo $directory . $dirArray[$index];?>"><?php echo $dirArray[$index];?></a></td>
<td><?php echo filetype($directory . $dirArray[$index]);?></td>
<td><?php echo filesize($directory . $dirArray[$index]);?></td>
</tr>
<?php
}
}
?>
</table>

bluestar_james
03-23-2011, 05:36 PM
I am using nearly identical script - question:

how would I modify the code to exclude index.php as well?

thanks!

bluewalrus
03-23-2011, 06:22 PM
At this line


if (substr("$dirArray[$index]", 0, 1) != "."){

You can add in restrictions


if (substr("$dirArray[$index]", 0, 1) != "."){
if ($dirArray[$index] != "index.php") {
//close it at the right location too

Not sure what those double quotes are there either, you could remove them to


if (substr($dirArray[$index], 0, 1) != "."){