digitalrjs Posted August 29, 2008 Share Posted August 29, 2008 I'm trying to list 3 files from a directory of 10 or 20 files. I just want to display only three with a limit amount of characters. This is the code I have so far but it list all the files. <?php echo ""; //The path to the style directory $dirpath = "pdf/"; $dl = opendir($dirpath); while (false !== ($file = readdir($dl))) { //admit sub directories if (!is_dir("$dirpath/$file")) { echo "$file"; } } closedir($dl); //Close Select ?> I was thinking adding a while loop and putting $file in an array. How can I display them in a list of 3 and with limit charactors. I can do this in asp but I love php. Quote Link to comment https://forums.phpfreaks.com/topic/121891-php-file-listing/ Share on other sites More sharing options...
sasa Posted August 29, 2008 Share Posted August 29, 2008 look <?php $count = 0; echo ""; //The path to the style directory $dirpath = "pdf/"; $dl = opendir($dirpath); while ($file = readdir($dl) and $count < 3) { //admit sub directories if (!is_dir("$dirpath/$file")) { if (strlen($file) < 7){ echo "$file"; $count++; } } } closedir($dl); //Close Select ?> Quote Link to comment https://forums.phpfreaks.com/topic/121891-php-file-listing/#findComment-628940 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 A way to limit files is to look at the glob() function, not only will it help with grabbing files, but it uses regex to find matches in file names. Honestly, glob() is an amazing tool for file management/manipulation. Quote Link to comment https://forums.phpfreaks.com/topic/121891-php-file-listing/#findComment-629128 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.