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
file listing in a directory trouble
Moderator: General Moderators
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
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
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
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