Tom8001 Posted August 9, 2015 Share Posted August 9, 2015 Hi How can i list files in directory that have a .html extension and get rid of . & .. Thanks, Help is very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/ Share on other sites More sharing options...
scootstah Posted August 9, 2015 Share Posted August 9, 2015 http://php.net/manual/en/function.glob.php Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518369 Share on other sites More sharing options...
Tom8001 Posted August 9, 2015 Author Share Posted August 9, 2015 <?php foreach (glob("*.html") as $filename) { echo $filename; } ?> How can i get this to list files in another directory? Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518372 Share on other sites More sharing options...
ginerjm Posted August 9, 2015 Share Posted August 9, 2015 Specify the path in the arguments to the glob call. Read the manual? Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518374 Share on other sites More sharing options...
Tom8001 Posted August 10, 2015 Author Share Posted August 10, 2015 It doesn't say anything about specifying the path in the manual for this function. Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518379 Share on other sites More sharing options...
Ch0cu3r Posted August 10, 2015 Share Posted August 10, 2015 It mentions it in the first sentence The glob() function searches for all the pathnames matching pattern The filepath is the pattern. Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518389 Share on other sites More sharing options...
ginerjm Posted August 10, 2015 Share Posted August 10, 2015 Reading is a real talent. Here's what one would read as soon as they referenced the manual: glob — Find pathnames matching a pattern Description array glob ( string $pattern [, int $flags = 0 ] ) 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. Also - if you scrolled down thru the examples you would find #6 from Alan.... that describes a pretty nifty function he wrote to do what you are possibly looking to do. No charge for the research. Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518396 Share on other sites More sharing options...
Tom8001 Posted August 10, 2015 Author Share Posted August 10, 2015 <?php $source_folder = "archive"; if( !is_dir( $source_folder ) ) { die ( "Invalid directory.\n\n" ); } glob($source_folder, "*.html"); ?> Warning: glob() expects parameter 2 to be long, string given Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518397 Share on other sites More sharing options...
Ch0cu3r Posted August 10, 2015 Share Posted August 10, 2015 First append the your folder name with a forward slash, $source_folder = "archive/"; Second use the concatenation operator (period . ) not a comma between $source_folder and "*.html" glob($source_folder. "*.html"); 1 Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518398 Share on other sites More sharing options...
Tom8001 Posted August 10, 2015 Author Share Posted August 10, 2015 Thanks, i got rid of the error but it's not listing any files. Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518403 Share on other sites More sharing options...
Ch0cu3r Posted August 10, 2015 Share Posted August 10, 2015 Calling glob on its own wont do any thing. You need the foreach loop like you had in post #3 1 Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518407 Share on other sites More sharing options...
Tom8001 Posted August 10, 2015 Author Share Posted August 10, 2015 Thanks everyone for helping me i appreciate it it's working now Quote Link to comment https://forums.phpfreaks.com/topic/297702-php-list-files-in-directory/#findComment-1518409 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.