Kevin McLean Posted January 16, 2009 Share Posted January 16, 2009 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/ Share on other sites More sharing options...
nrg_alpha Posted January 16, 2009 Share Posted January 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738768 Share on other sites More sharing options...
.josh Posted January 16, 2009 Share Posted January 16, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738776 Share on other sites More sharing options...
nrg_alpha Posted January 16, 2009 Share Posted January 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738781 Share on other sites More sharing options...
.josh Posted January 16, 2009 Share Posted January 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738792 Share on other sites More sharing options...
nrg_alpha Posted January 17, 2009 Share Posted January 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738815 Share on other sites More sharing options...
.josh Posted January 17, 2009 Share Posted January 17, 2009 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738816 Share on other sites More sharing options...
nrg_alpha Posted January 17, 2009 Share Posted January 17, 2009 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 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738824 Share on other sites More sharing options...
.josh Posted January 17, 2009 Share Posted January 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738841 Share on other sites More sharing options...
nrg_alpha Posted January 17, 2009 Share Posted January 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141139-displaying-file-in-a-directory/#findComment-738852 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.