PHP Directory listing script<

liunx

Guest
Hi everyone,

I have this so far.


if ($handle = @opendir('users/' . $un)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." || $file != "..") {


I got this


if ($file != "." || $file != "..")


From the php manual. Actually it has an &&, but either way this doesn't seem to filter out files like .htaccess for me. :( Ends up listing everything, even .htacess.

Any ideas?

ThanksHmm, based on the thread title I was originally going to refer you to the thread over at: <!-- m --><a class="postlink" href="http://www.htmlforums.com/showthread.php?s=&threadid=32868">http://www.htmlforums.com/showthread.ph ... adid=32868</a><!-- m --> but that isn't quite relevant to what you want to know.


The test for the "." and ".." is to remove the references to the "current directory" and "parent directory" from your listings. Those are system entries, rather than user files and folders.

You could add this: && (file != ".htaccess") to the test to filter that filename out too.

You do need the "AND" ( && ) between each part, rather than the "OR" function.something like
$file !='.*'

and i have a question:
why
while (false !== ($file = readdir($handle))) {
instead of
while ($file = readdir($handle)) {

??Here, this is what I use:

<?php
function listFiles($dir='.')
{
$res = array();
if ( $handle = opendir($dir) )
{
while ( false !== ($file = readdir($handle)) )
{
if ( is_file($file) )
{
$res[] = array('file' => $file,
'size' => filesize($dir . "/". $file),
'filemtime' => filemtime($dir . "/". $file)
);
}
}
closedir($handle);
}
return $res;
}
?>

This is another one I found:

<?php
$files = listFiles();
foreach($files as $file)
{
printf("%s is %d bytes\n",$file['file'],$file['size']);
}
?>Thanks guys, I haven't had a chance to try any of these yet. I'll check them out soon.

illogique

PHP.net manual says your first way of doing it is not correct. I'm not sure why.

You can read here

<!-- m --><a class="postlink" href="http://ca3.php.net/manual/en/function.readdir.phpOriginally">http://ca3.php.net/manual/en/function.r ... Originally</a><!-- m --> posted by illogique
something like
$file !='.*'

and i have a question:
why
while (false !== ($file = readdir($handle))) {
instead of
while ($file = readdir($handle)) {

??
you can't use a wildcard in a if statement. * means nothing to php. if you do that you have to use regexp.

teh second one is doing a reference, have you tried your way? bet it won't work. and it is better to write it this way

while (($file = readdir($handle)) !== false) {

cause you want to reference $file to false, not false to $file$a !== $bNot identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)

so
a!==b
is the same as
b!==a

and it does work without the false !==
from the test i just did,
if a file is name '0' then it end up the while since it does not know if it's 0 or '0' or false, that why (as i found out) you need to compare type and not only value...Another question somewhat related to this post. If I wanted the user only to be able to get the files I listed how would I do this? In other words if the user types in the file into the browser they do not get the files? Would I password protect the directory and get php to log into it somehow or store the files outside of the web home directory? I鎶
 
Back
Top