Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/185711-echo-folder-contents/
Share on other sites

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('.'));

Link to comment
https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-980905
Share on other sites

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. :shy:

 

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";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-980930
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-981046
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-981057
Share on other sites

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 :-)

Link to comment
https://forums.phpfreaks.com/topic/185711-echo-folder-contents/#findComment-981095
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.