Author Topic: displaying file in a directory  (Read 685 times)

0 Members and 1 Guest are viewing this topic.

Offline Kevin McLeanTopic starter

  • Irregular
  • Posts: 12
    • View Profile
displaying file in a directory
« on: January 16, 2009, 05:15:28 PM »
I've got a script which displays files in directory. But I strongly need it to display files only with names like
number.php
so it would display files which contain only digits in its name and .php. I know that it could be achieved using regex, but my knowledge about them is practically zero, could somebody, please help me to modify the script.
Code: [Select]
<?

$path = '/.../archive/';
$file = scandir($path);
unset($file[0], $file[1]); 
function _sortCTime($file1, $file2){
    $time1 = filectime('archive/' . $file1);
    $time2 = filectime('archive/' . $file2);
    if($time1 == $time2)
      return 0;
    return ($time1 < $time2) ? -1 : 1;
  } 


$i=1;
$f = 'семестр';
usort($file, "_sortCTime");
foreach($file as $item){
   
            $i++;
            echo "$i $f. <a href='archive/$item'>Успеваемость</a><br />";

    } 

?>

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: displaying file in a directory
« Reply #1 on: January 16, 2009, 06:25:34 PM »
Assumes $path points to the correct folder...


$path 
'/.../archive/';
foreach(
glob($path "*.php") as $fileName){
   
$file basename($fileName);
   if(
preg_match('#^[\d]+\.php$#'$file)){
      
$fileList[] = $file;
   }
}
echo 
'<pre>'.print_r($fileListtrue);


From here, simply plug the array of file names into the script as needed.
« Last Edit: January 16, 2009, 06:31:44 PM by nrg_alpha »
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,150
  • Grumpy Old Man
    • View Profile
Re: displaying file in a directory
« Reply #2 on: January 16, 2009, 06:38:06 PM »
glob does support character classes, so you can skip doing a *.php and preg_matching all the files:


$list 
glob("[0-9]*.php");
echo 
"<pre>"print_r($list); echo "</pre>";
« Last Edit: January 16, 2009, 06:38:38 PM by Crayon Violent »

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: displaying file in a directory
« Reply #3 on: January 16, 2009, 06:43:45 PM »
glob does support character classes, so you can skip doing a *.php and preg_matching all the files:

So I see... when I checked up in the manual, the wording gave me the impression it didn't.
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,150
  • Grumpy Old Man
    • View Profile
Re: displaying file in a directory
« Reply #4 on: January 16, 2009, 06:55:39 PM »
I'd say it's more vague or maybe incomplete than misleading...

From the manual:
Quote
The glob() function searches for all the pathnames matching pattern  according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

I think it should actually show examples rather than giving a vague reference, especially since it says it matches according to 'similar' rules and not 'exact' rules.

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: displaying file in a directory
« Reply #5 on: January 16, 2009, 07:23:53 PM »
I think it should actually show examples rather than giving a vague reference, especially since it says it matches according to 'similar' rules and not 'exact' rules.

I agree... once I didn't find the wording very helpful, the next thing I did was start scanning down the list of examples.. but kept seeing *.php or something to that effect.. so this kind of further 're-enforced' my suspicions that I could not get away with classes..

They say hind sight has 20/20 vision however.. perhaps I should have spent a few extra seconds actually trying it out, just in case. But yeah, I agree with you 100%. You would think the PHP manual would show some seriously diverse applications of using glob with all it's flexibility. :(
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,150
  • Grumpy Old Man
    • View Profile
Re: displaying file in a directory
« Reply #6 on: January 16, 2009, 07:26:52 PM »
On a positive note, it seems like a chance to immortalize yourself in the php manual, by posting an entry explaining what exactly is allowed :P

Because I'm gracious and generous, I will allow you to have that honor.  My infamy already causes me to beat off the women with a stick :D
« Last Edit: January 16, 2009, 07:28:06 PM by Crayon Violent »

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: displaying file in a directory
« Reply #7 on: January 16, 2009, 07:41:34 PM »
On a positive note, it seems like a chance to immortalize yourself in the php manual, by posting an entry explaining what exactly is allowed :P

Because I'm gracious and generous, I will allow you to have that honor.  My infamy already causes me to beat off the women with a stick :D

lol you're so kind. I rescanned the samples, and they do show a version involving character classes afterall (I must have missed it):


$dir 
'./mydir/[with]/special/.chars/';
$file '[a-z]+\.txt';

$files globpreg_quote$dirDIRECTORY_SEPARATOR ) . $file );

if(
$files !== false && count($files) != 0)
{
    
// Do something with the files
}


D'oh! Now I really feel stupid :( Pass one of them women you had to fend off... I could use a hug right about now lol

Granted, I do think some of these examples should have been shown near the top, where everyone can see them, as opposed to being included as some user's commented demonstration. But alas... I suppose that's why those user entries are there for.
« Last Edit: January 16, 2009, 07:45:58 PM by nrg_alpha »
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,150
  • Grumpy Old Man
    • View Profile
Re: displaying file in a directory
« Reply #8 on: January 16, 2009, 08:04:15 PM »
You know, I saw that manual entry.  glob doesn't recognize the +.  Only * or ? but from a shell context.  Like in a shell context, ? is really like a dot (.) in that it matches any one character (other than a new line) and * is the same: 0 or more.  But + (1 or more) is not supported.  I tried it even from a bash prompt and no dice on the +.  So yeah, that user comment is wrong.

Also * acts on its own accord. So for instance, if I did a*.php the * is independent of the 'a'. glob("a*.php") will return all of the following, but the * itself will only match:

a.php // * matches nothing
aa.php // * matches the 2nd a
ab.php // * matches 'b'
abc.php // * matches 'bc'

As far as the ? goes, like I said, it's really like a "pcre" dot (.) in that it matches any one character (not 0 or 1) so if you did

glob("a?.php"); against
a.php
aa.php
ab.php
abc.php

aa.php and ab.php would be returned.

« Last Edit: January 17, 2009, 12:16:46 AM by Crayon Violent »

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: displaying file in a directory
« Reply #9 on: January 16, 2009, 08:34:25 PM »
Ah, I see.. a little song and dance going on that mimics PCRE to some degree, but perhaps not quite in the sense we would think it would.
Duly noted :)
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!