Readfiles Script
From Superk
This script is really rough and will read in both regular files as well as directories. Need to do more research on the readdir() function.
#!/usr/bin/php
<?php
// Set the directory to read ('/' for Unix & '\\' for Windows (I think))
$dir = '/home/bkrein';
// If the directory exists, run the loop
if($loc = opendir($dir)) {
echo "Files: \n\n";
// Loop through the files in the $dir
// NOTE: '!==' only works on PHP 4+
while(false !== ($file = readdir($loc))) {
// If the filename matches the expression, display it
if((preg_match("/update/", $file))&&(filetype($file) == 'file')) {
echo "<a href=\"$dir/$file\">$file</a>\n";
}
}
// Close the directory (cleanup)
closedir($loc);
}
?>
Produces this output:
bkrein@bkreinlap:~$ php readfiles.php Files: <a href="/home/bkrein/update-1.4.3.tgz.gpg">update-1.4.3.tgz.gpg</a> <a href="/home/bkrein/update-1.4.4.tgz.gpg">update-1.4.4.tgz.gpg</a> <a href="/home/bkrein/update-1.4.5.tgz.gpg">update-1.4.5.tgz.gpg</a>
The following code is similar to the above, but reads the filename and date into an array then sorts the array based on the timestamp:
#!/usr/bin/php
<?php
// Set the directory to read ('/' for Unix & '\\' for Windows (I think))
$dir = '/home/bkrein';
function cmp($a, $b) {
return strcmp($a['date'], $b['date']);
}
// If the directory exists, run the loop
if($loc = opendir($dir)) {
echo "Files: \n\n";
// Loop through the files in the $dir
// NOTE: '!==' only works on PHP 4+
while(false !== ($file = readdir($loc))) {
// If the filename matches the expression, display it
if((preg_match("/[0-9]{2}/", $file))&&(filetype($file) == 'file')) {
$files[] = array('file' => $file, 'date' => filemtime($file));
}
}
// Close the directory (cleanup)
closedir($loc);
usort($files, 'cmp');
foreach($files as $item) {
echo '<a href="'.$dir.'/'.$item['file'].'">'.$item['file'].'</a> - Last modified on: '.date("F d, Y", strtotime($file['date']))." (Timestamp: ".$item['date'].")\n";
}
}
?>
This script outputs:
bkrein@bkreinlap:~$ php readfiles.php Files: <a href="/home/bkrein/01.txt">01.txt</a> - Last modified on: May 13, 2005 (Timestamp: 1116003816) <a href="/home/bkrein/03.txt">03.txt</a> - Last modified on: May 13, 2005 (Timestamp: 1116003820) <a href="/home/bkrein/02.txt">02.txt</a> - Last modified on: May 13, 2005 (Timestamp: 1116003823)
