Forum Moderators: coopster
<?
$dir = "/foo/bar";
$pattern = '^(amsn[^/.]+\.deb)$';
$newstamp = 0;
$newname = "";
$dc = opendir($dir);
while ($fn = readdir($dc)) {
//echo $fn;
# Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fn)) continue;
# Eliminate other pages not in pattern
if (! preg_match($pattern,$fn)) continue;
$timedat = filemtime("$dir/$fn");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fn;
}
}
# $timedat is the time for the latest file
# $newname is the name of the latest file
echo $timedat;
echo "<br />";
echo $newname;
?>
Unfortunately I get that unknown modifier error, please do you have any hints?
[edited by: phranque at 11:14 pm (utc) on Mar. 25, 2009]
[edit reason] No urls, please. See TOS [webmasterworld.com] [/edit]
[^/.]
i'm guessing you should try [^.] or [^\/.] here
Special Characters Inside a Bracketed Character Class [search.cpan.org]:
Characters that may carry a special meaning inside a character class are: \, ^, -, [ and ], and are discussed below. They can be escaped with a backslash, although this is sometimes not needed, in which case the backslash may be omitted.
changing it with:
if ( preg_match( '/^\.\.?$/', $fn ) ) continue;
solves finally everything and the script does its job.
It seems like some / were missing; regex are relatively new for me, I hope I can improve my skill on them in the future.
Thanks
<?php
$dir = "/foo/bar";
$pattern = '/^(amsn_0.9[^\/]+.deb)$/';$newstamp = 0;
$timedat = 0;
$newname = "";
$dc = opendir($dir);
while ($fn = readdir($dc)) {
# Eliminate current directory, parent directory
if ( preg_match( '/^..?$/', $fn ) ) continue;
# Eliminate other pages not in pattern
if (! preg_match($pattern,$fn)) continue;
$timedat = filemtime("$dir/$fn");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fn;
}
}
# $timedat is the time for the latest file
# $newname is the name of the latest file
$scrivi_data = date('j M y', $timedat);
echo "Versione pił recente compilata il: <strong> $scrivi_data </strong>";
echo "Nome: ";
echo "<strong> $newname </strong>";
?>
#newname works good and outputs the most recent filename, but #timedat doesn't show the most recent date
If I have a folder with two files:
amsn1.deb 3 apr
amsn2.deb 6 apr, the script will show:
The most recent file is amsn2.deb
Compiled on $amsn1.deb_date
While it should show the date in which amsn2.deb has been compiled.
Any hints?
thanks