Jump to content

displaying file in a directory


Kevin McLean

Recommended Posts

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.

<?

$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 />"; 

    }  

?>

Link to comment
https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/
Share on other sites

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($fileList, true);

 

From here, simply plug the array of file names into the script as needed.

I'd say it's more vague or maybe incomplete than misleading...

 

From the manual:

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.

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. :(

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

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 = glob( preg_quote( $dir, DIRECTORY_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.

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.