Page 1 of 1

file listing in a directory trouble

Posted: Wed May 15, 2002 5:03 am
by metuz
got a little bit of a problem, I'm new to php so the code might be ugly..

<?
$handle=opendir('files');
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "") {
$filename = str_replace("_"," ",$file);
$filemod = filemtime($file);
$filemodtime = date("Y-m-d H:i:s", $filemod);
$filename = str_replace(".pdf","",$filename);
$filename = str_replace(".doc","",$filename);
echo "<li><a href=filer/$file target=_blank>$filename</a> - ";
echo "Last mod: " . $filemodtime;
echo "</li>";
}
}
closedir($handle);
?>


It lists the contents of the direcory nicely, but the last modification date
says 1970-01-01 01:00:00 , what am I doing wrong.. need help

Posted: Wed May 15, 2002 5:59 am
by mikeq
I calculate file mod date/times in the same way and they are okay.

I can only guess that you are not actually passing the correct filename to the function.

$filename = str_replace("_"," ",$file);
$filemod = filemtime($file);

Does $file have the full path to the file or is it just the file name. I think it has to be the full path if the files are not in the directory your script runs in, can someone else confirm this?

Do a
Print "The file is: $file";
somewhere in your loop
just to make sure $file contains what you assume it does.

Mike

Posted: Thu May 16, 2002 12:34 am
by metuz
well, you where right about $file not having full path, but I can't
get $file to include the full path, any tips how I do that..

Posted: Thu May 16, 2002 4:21 am
by mikeq
Maybe relative paths would work also. You are opening the directory to read the files so where is the directory 'files' in relation to your running script.

If it is back up one then something like ../files/myfile.doc may work. Or if it is simply below your current directory then files/myfile.doc should work. So just prefix your $file variable

$file = "files/$file";
or set a Constant to the directory and use that

$file = $MyDirConstant.$file;

Mike

Posted: Thu May 16, 2002 5:12 am
by metuz
thank you, got it to work like I wanted now :D