php-n00b Posted December 19, 2009 Share Posted December 19, 2009 What's Up? OK, So I'm finally nearing the end of my first big php endeavour, The last thing I need to do it to make viewing all the files I have easier, Is it possible to echo all the files (they all end in .html) in a php file? So something like. $var = "Folder/*.html"; echo $var But not that of course. Any ideas? Thanks Bye! Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/ Share on other sites More sharing options...
ignace Posted December 19, 2009 Share Posted December 19, 2009 function my_echo($s) { echo $s, '<br />'; } array_map('my_echo', scandir('.')); Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-980615 Share on other sites More sharing options...
php-n00b Posted December 19, 2009 Author Share Posted December 19, 2009 function my_echo($s) { echo $s, '<br />'; } array_map('my_echo', scandir('.')); Oops I meant to echo the contents of all the files, Why am I such an idiot? Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-980623 Share on other sites More sharing options...
ignace Posted December 20, 2009 Share Posted December 20, 2009 function get_directory_contents($directory, $sorting_order = 0, $context = null) { $files = array(); $directory = rtrim($directory, '\/') . DIRECTORY_SEPARATOR; if ($scan =@ scandir($directory, $sorting_order, $context)) { foreach ($scan as $file) { $files[] = $directory . $file; } } return $files; } function print_file_contents($file) { print file_get_contents($file); } array_map('print_file_contents', get_directory_contents('.')); Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-980905 Share on other sites More sharing options...
salathe Posted December 20, 2009 Share Posted December 20, 2009 With PHP 5.3 came a lovely new addition to the SPL; GlobIterator. If PHP 5.3 is available to then read on, otherwise skip to the part of this post entitled PHP 5.2.0 or above. If you're using a PHP version below that, I'd urge an upgrade. GlobIterator (PHP 5.3 or above) You may not already be aware of it, but there is a core PHP function named glob which allows searching for files (including directories, etc.) matching a basic pattern (like your "Folder/*.html"). This function returns an array of matching files. The GlobIterator class takes this idea and folds it into an object-oriented wrapper which makes looping and working with the files (fairly) simple. The following is an example of using the GlobIterator to list the HTML files in the "Folder" folder. Note: It does not echo the files' contents (that comes later). <?php $glob = new GlobIterator('Folder/*.html'); foreach ($glob as $file) { echo $file->getFilename() . "\n"; } ?> PHP 5.2.0 or above Since the GlobIterator isn't available until recently, we can fall back on classes that have been available for a while. In this case, I'll be making use of the DirectoryIterator and RegexIterator to replicate what the GlobIterator is doing. There are yet more ways to use the SPL classes to filter directory contents in older versions (of PHP 5) but I'd hope you're using at least 5.2. So, this example uses a regular expression to filter the file names to only those ending in ".html". <?php $glob = new RegexIterator(new DirectoryIterator('Folder/'), '/\.html$/D'); foreach ($glob as $file) { echo $file->getFilename() . "\n"; } ?> The examples obviously don't echo the files' contents. That's simple enough to do (in a variety of ways) either with the old-school file_get_contents (or even older fopen/fgets/fclose combo) or the more in-theme SPL way. The latter would be use do something like the following, inside the loop. $lines = $file->openFile(); foreach ($lines as $line) { echo $line; } This makes use of the SplFileObject class (which no-one ever uses) to save reading the entire file into memory at once as would happen with file_get_contents(). Putting that all together, a quick script to find all of the HTML files in the "Folder" folder and output their contents might look like: <?php $glob = new RegexIterator(new DirectoryIterator("Folder/"), '#\.html$#D'); foreach ($glob as $file) { echo $file->getFilename() . "\n"; $lines = $file->openFile(); foreach ($lines as $line) { echo $line; } echo "\n--\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-980930 Share on other sites More sharing options...
ignace Posted December 20, 2009 Share Posted December 20, 2009 @salathe like you said GlobIterator is a glob wrapper. Thus if it doesn't exist you can create it yourself: if (!class_exists('GlobIterator')) { class GlobIterator extends FilesystemIterator implements Iterator, Traversable, SeekableIterator, Countable { //.. } } Assuming FilesystemIterator does exists the manual does not point out when which component was added to the SPL. Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-981046 Share on other sites More sharing options...
salathe Posted December 20, 2009 Share Posted December 20, 2009 Assuming FilesystemIterator does exists the manual does not point out when which component was added to the SPL. That's something we've discussed: displaying the version info on the class overview page. In the mean time, click through to one of the methods of that class (__construct is usually a good choice if available) and the version info should be there. The FilesystemIterator was added in 5.3, like GlobIterator, but many of the other iterators are available for earlier versions. Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-981057 Share on other sites More sharing options...
php-n00b Posted December 20, 2009 Author Share Posted December 20, 2009 Thanks everyone for helping, I used $glob = new RegexIterator(new DirectoryIterator("Folder/"), '#\.html$#D'); foreach ($glob as $file) { echo $file->getFilename() . "\n"; $lines = $file->openFile(); foreach ($lines as $line) { echo $line; } echo "\n--\n"; } using that glob thing i got some sort of error, Now i see the contents of the files all in one place, Thanks alot :-) Quote Link to comment https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-981095 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.