jaaames Posted October 31, 2006 Share Posted October 31, 2006 I'm not sure how complex this is, but I'm relatively new to PHP, so I'll ask anyways.Let's say I have a directory of text files, each containing information. And I'd like to list them in an array, for later manipulation.How could I do this? I'm not very knowledgeable when it comes to file I/O. Please help? Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/ Share on other sites More sharing options...
bqallover Posted October 31, 2006 Share Posted October 31, 2006 Do you mean list the filenames in an array or list the contents of the files in an array? Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117085 Share on other sites More sharing options...
jaaames Posted October 31, 2006 Author Share Posted October 31, 2006 List the filenames in an array. Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117086 Share on other sites More sharing options...
bqallover Posted October 31, 2006 Share Posted October 31, 2006 This should do the trick! $dir is the path to the required directory. At the end, $file_list is an array of all filenames in the directory.[code]if($handle = opendir($dir)){ while(($file = readdir($handle)) !== false) { if( $file != "." && $file != ".." ) { $file_list[] = $file; } } closedir($handle);}[/code] Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117093 Share on other sites More sharing options...
jaaames Posted October 31, 2006 Author Share Posted October 31, 2006 Thank you, that worked just fine!Okay, now here's a related problem: How do I list the values for keys 1-5 only? (Sorry for the stupid questions.) Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117109 Share on other sites More sharing options...
brendandonhue Posted October 31, 2006 Share Posted October 31, 2006 You could just do $array = glob('*.txt'); to get an array with the names of all the text files. You can use a loop like for() to display only the first 5. Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117110 Share on other sites More sharing options...
bqallover Posted October 31, 2006 Share Posted October 31, 2006 NOT asking would be stupid. ;)What you need is a 'for loop' (read about them at [url=http://uk.php.net/manual/en/control-structures.for.php]http://uk.php.net/manual/en/control-structures.for.php[/url])[code]for( $i=1; $i<=5; $i++ ){ echo $file_list[$i] . "\n" // print filename and a new-line}[/code]Be aware though that arrays are generally 0-indexed, meaning the first filename is at $file_list[0]. In that case change the for loop to[code]for ($i=0, $i<=4; $i++ )[/code]There are simpler (better?) control structures like 'while' and 'foreach', but for something like this I'd use 'for' Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117112 Share on other sites More sharing options...
bqallover Posted October 31, 2006 Share Posted October 31, 2006 [quote author=brendandonhue link=topic=113297.msg460344#msg460344 date=1162263316]You could just do $array = glob('*.txt'); to get an array with the names of all the text files. You can use a loop like for() to display only the first 5.[/quote]Yeah, probably a better technique. Head like a sieve me... :D Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117114 Share on other sites More sharing options...
jaaames Posted October 31, 2006 Author Share Posted October 31, 2006 Thanks, the for() loop worked just fine (and thanks for teaching me something new). Link to comment https://forums.phpfreaks.com/topic/25651-print-directory-listing-to-array/#findComment-117129 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.