How do I display the contents of each text file in a directory?
I created a directory with some files in there:
- index.php
- one.txt
- two.txt
- three.txt
- four.txt
On the page index.php
I am currently using this code to display all files in a directory:
<?php
$blacklist = array("index.php");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
echo "$entry\n";
}
}
closedir($handle);
}
?>
Now if anyone views the page index.php
, this is what they see:
one.txt two.txt three.txt four.txt
As you can see from the PHP code, it index.php
is blacklisted so it will not be displayed.
However, I would like to go a step further than this and output the contents of each text file, not the file names. With new PHP code (I need help building), when someone visits the page index.php
, this is what they now see:
(Please ignore what is in the asterisks, they are not part of the code, they just indicate what each text file contains)
Hello ** this is what the file **one.txt** contains **
ok ** this is what the file **two.txt** contains **
goodbye ** this is what the file **three.txt** contains **
text ** this is what the file **four.txt** contains **
Generally:
I would like to highlight the contents of every file in a directory (all text files) except index.php
.
You can use file_get_contents
to put the file on a line.
<?php
$blacklist = array("index.php");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
echo "$entry " . file_get_contents($entry) . "\n";
}
}
closedir($handle);
}
?>
In addition, you can use PHP's function glob
to filter only files .txt
, so you don't need to be blacklisted if you are going to add more files to this directory to be ignored.
This is how it would be done with a function glob
.
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename " . file_get_contents($filename) . "\n";
}
?>
source to share
This will print the contents of the files. You can do a workaround if the path is not the current path and is recording some sort of boundary between the contents of the files.
<?php
$blacklist = array("index.php");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
echo file_get_contents($entry) . "\n";
}
}
closedir($handle);
}
?>
Hope this helps you.
source to share
Never reinvent the wheel. Use a composer.
Require symfony / finder
use Symfony\Component\Finder\Finder;
class Foo
{
public function getTextFileContents($dir)
{
$finder = (new Finder())->files()->name('*.txt');
foreach ($finder->in($dir) as $file) {
$contents = $file->getContents();
// do something while file contents...
}
}
}
source to share
I would let some SPL filesystem iterators do the following:
$dir = '/home/mydirectory';
$rdi = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
$rdi = new \RegexIterator($rdi, '/\.txt$/i');
$iterator = new \RecursiveIteratorIterator($rdi, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
echo 'Contents of the '.$file->getPathname().' is: ';
echo file_get_contents($file->getPathname());
}
This will recursively find and iterate over all files .txt
in a given directory, including subdirectories.
Since each iteration $file
is an instance FilesystemIterator
, you can use all related methods for additional controls like $file->isLink()
(true for symbolic links), $file->isReadable()
(false for unreadable files), etc.
If you don't want the substitution subfolders, just change the RecursiveDirectoryIterator on the second line:
$rdi = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
in
$rdi = new \DirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
Hope it helps.
source to share
As @ brock-b said, you can use glob to get the full list of files and file_get_contents to grab the contents:
$blacklist = array('index.php');
$files = glob('*.txt'); # could be *.* if needed
foreach ($files as $file) {
if (!in_array(basename($file), $blacklist)) {
echo file_get_contents($file);
}
}
Note: the blacklist will not be removed as you are looking for files *.txt
. Useful when performing file searches *.*
or*.php
source to share