Web Hosting Talk







View Full Version : Post date of file


blockcipher
05-05-2005, 07:19 PM
Hello everyone :)

I'm not a programmer but grabbed some php code of the net that will list out files in a folder. Below is a sample of that code:



<?php


// open this directory
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory))
{
$dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);


// count elements in array
$indexCount = count($dirArray);
sort($dirArray);
print("<TABLE border=0 cellpadding=0 cellspacing=0 \n");


for($index=0; $index< $indexCount; $index++)
{

print("<tr><td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td></a></td> ");
print("<td>");

print("<td>");
print("</td>");
print("</TR>\n");
}
print("</TABLE>\n")





?>




Now its listing the files great, but now I want to somehow echo out the date of the file.

Any programmer out there willing to give me the code? ;) I searched awhile on google, but I'm probably putting in the wrong info :)

I appreciate it!

blockcipher
05-05-2005, 07:42 PM
Update:

I have tried to use

echo "Last modified: ".date( "F d Y H:i:s.", getlastmod() );

and

echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));

But, it shows todays date on all files.

Sorry ;)

t3r0
05-05-2005, 07:43 PM
<?php

// date format see http://php.net/date
$dateformat = "r";

// open this directory
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory))
{
$dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);


// count elements in array
$indexCount = count($dirArray);
sort($dirArray);

echo "<TABLE border=0 cellpadding=0 cellspacing=0 >\n";


for($index=0; $index< $indexCount; $index++) {
echo "\t<tr><td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td><td>" . date($dateformat,filemtime($dirArray[$index])) . "</td></tr>\n";

}
echo "</TABLE>\n";


?>


Small modification to the original :)

- Tero

blockcipher
05-05-2005, 07:47 PM
t3r0,

You are the BEST!!! That worked perfectly :)

I really appreciate this.

blockcipher
05-06-2005, 11:39 AM
t3r0,

Is there also a way to only list a certain file type? I have a directory full of PDF documents that I'm listing, with the code you provided above. But it also lists out the actual php files in the directory as well.

Thanks for any tips! :D

zupanm
05-06-2005, 01:01 PM
an easy way is to use explode(".", $file) then read the extention.. if its a pdf.. then process

t3r0
05-06-2005, 06:28 PM
Hi,

Change


while($entryName = readdir($myDirectory))
{
$dirArray[] = $entryName;
}

To:

while($entryName = readdir($myDirectory))
{
if (eregi(".pdf", $entryName))
$dirArray[] = $entryName;
}




- Tero

t3r0
05-06-2005, 06:48 PM
Or even better solution is to change the whole script to:



<table border="1" cellpadding="5" cellspacing="5">

<?php
foreach ( glob("{*.pdf,*.PDF}", GLOB_BRACE) as $file) {
echo "\t<tr><td><a href=\"$file\">$file</a></td><td>" . date("r",filemtime($file)) . "</td></tr>\n";
}
?>

</table>

blockcipher
05-06-2005, 07:00 PM
I tried messing around with explode, but man I don't know what I"m doing! ahha. t3r0, that worked great.

Thanks once again!