file listing in a directory trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
metuz
Forum Newbie
Posts: 4
Joined: Wed May 15, 2002 5:03 am
Location: sweden
Contact:

file listing in a directory trouble

Post 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
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
metuz
Forum Newbie
Posts: 4
Joined: Wed May 15, 2002 5:03 am
Location: sweden
Contact:

Post 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..
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
metuz
Forum Newbie
Posts: 4
Joined: Wed May 15, 2002 5:03 am
Location: sweden
Contact:

Post by metuz »

thank you, got it to work like I wanted now :D
Post Reply