I need to load index.php file from each of the plugins' folders. There is the main folder "plugins" and inside there are, sub folders (plugins) e.g blog, members etc. Inside each plugin folder there is an index.php file which i need to load. How can i load the directory and search for these files. The plugin folders are not static and might change.
What i have tried
$dir_iterator = new RecursiveDirectoryIterator($this->plugin_dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wish
foreach ($iterator as $file) {
echo $file, "\n";
}
and..the glob function (which didn't help much
$list = glob('index.php', GLOB_BRACE);
foreach($list as $files){
echo $files;
}
print_r($list);
I used a double listing way..
private function loadPlugins(){
$dir = array_diff(scandir($this->plugin_dir), array('..', '.'));
foreach($dir as $ds){
$list = glob($this->plugin_dir.'/'.$ds.'/index.php', GLOB_BRACE);
foreach($list as $files){
require $files;
}
}
}
Using the unlink function in php is it possible to search a directory with multiple folders for txt files with a certain name. In my case Newsfeed.txt
Where should I start with this ?
Great answer maxhb. Here's something a little more manual.
<?php
function unlink_newsfeed($checkThisPath) {
$undesiredFileName = 'Newsfeed.txt';
foreach(scandir($checkThisPath) as $path) {
if (preg_match('/^(\.|\.\.)$/', $path)) {
continue;
}
if (is_dir("$checkThisPath/$path")) {
unlink_newsfeed("$checkThisPath/$path");
} else if (preg_match( "/$undesiredFileName$/", $path)) {
unlink("$checkThisPath/$path");
}
}
}
unlink_newsfeed(__DIR__);
You could use the recursive directory iterators of the php standard library (SPL).
function deleteFileRecursive($path, $filename) {
$dirIterator = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator(
$dirIterator,
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if(basename($file) == $filename) unlink($file);
}
}
deleteFileRecursive('/path/to/delete/from/', 'Newsfeed.txt');
This will allow you to delete all files with name Newsfeed.txt from the given folder and all subfolders.
I have this simple below code to help me get the contents of all files and sub folder. I took this code from the PHP cook book. it says that I can use the isDot() but it doesn't explain how to ..
I tried to put a if just before array but it doesn't work...
if (! $file->isDot())
I don't know how to put both getPathname and isDot when adding to the array.
I've read the PHP net website but don't understand how to fit in.
code:
$dir = new RecursiveDirectoryIterator('/usr/local');
$dircontent = array();
foreach (new RecursiveIteratorIterator($dir) as $file) {
$dircontent [] = $file -> getPathname();
}
print_r($dircontent);
Use the FilesystemIterator::SKIP_DOTS flag in your RecursiveDirectoryIterator to get rid of dot files:
$dir = '/usr/local';
$result = array();
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$dir,
FilesystemIterator::SKIP_DOTS
)
);
foreach ($files as $file) {
$result[] = $file->getPathname();
}
print_r($result);
The reason isDot() is failing is that your $file variable holds a SplFileInfo object, not a DirectoryIterator.
Im using this code:
$path = realpath('');
$i = 0;
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach($objects as $name => $object){
and it gets all sub directories in the main directory, when I only want it to get in the current directory
How would I go about doing that?
You could make use of glob() instead.
<?php
chdir('yourcurrentdirectory');
foreach(glob("*.*") as $file)
{
echo $files."<br>";
}
I'm trying to return the files in a specified directory using a recursive search.
I successfully achieved this, however I want to add a few lines of code that will allow me to specify certain extensions that I want to be returned.
For example return only .jpg files in the directory.
Here's my code,
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
foreach(new RecursiveIteratorIterator($it) as $file) {
echo $file . "<br/> \n";
}
?>
please let me know what I can add to the above code to achieve this, thanks
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$display = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)
{
if (in_array(strtolower(array_pop(explode('.', $file))), $display))
echo $file . "<br/> \n";
}
?>
You should create a filter:
class JpegOnlyFilter extends RecursiveFilterIterator
{
public function __construct($iterator)
{
parent::__construct($iterator);
}
public function accept()
{
return $this->current()->isFile() && preg_match("/\.jpe?g$/ui", $this->getFilename());
}
public function __toString()
{
return $this->current()->getFilename();
}
}
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$it = new JpegOnlyFilter($it);
$it = new RecursiveIteratorIterator($it);
foreach ($it as $file)
...
Try this, it uses an array of allowed file types and only echos out the file if the file extension exists within the array.
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$allowed=array("pdf","txt");
foreach(new RecursiveIteratorIterator($it) as $file) {
if(in_array(substr($file, strrpos($file, '.') + 1),$allowed)) {
echo $file . "<br/> \n";
}
}
?>
You may also find that you could pass an array of allowed file types to your RecursiveDirectoryIterator class and only return files that match.
Let PHP do the job:
$directory = new RecursiveDirectoryIterator('path/to/directory/');
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/\.jpe?g$/i', RecursiveRegexIterator::GET_MATCH);
echo '<pre>';
print_r($regex);
Regarding the top voted answer: I created this code which is using fewer functions, only 3, isset(), array_flip(), explode() instead of 4 functions. I tested the top voted answer and it was slower than mine. I suggest giving mine a try:
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
foreach(new RecursiveIteratorIterator($it) as $file) {
$FILE = array_flip(explode('.', $file));
if (isset($FILE['php']) || isset($FILE['jpg'])) {
echo $file. "<br />";
}
}
None of these worked for my case. So i wrote this function, not sure about efficiency, I just wanted to remove some duplicate photos quickly. Hope it helps someone else.
function rglob($dir, $pattern, $matches=array())
{
$dir_list = glob($dir . '*/');
$pattern_match = glob($dir . $pattern);
$matches = array_merge($matches, $pattern_match);
foreach($dir_list as $directory)
{
$matches = rglob($directory, $pattern, $matches);
}
return $matches;
}
$matches = rglob("C:/Bridge/", '*(2).ARW');
Only slight improvement that could be made is that currently for it to work you have to have a trailing forward slash on the start directory.
In this sample code;
You can set any path for $directory variable, for example ./, 'L:\folder\folder\folder' or anythings...
And also you can set a pattern file name in $pattern optional, You can put '/\.jpg$/' for every file with .jpg extension, and also if you want to find all files, can just use '//' for this variable.
$directory = 'L:\folder\folder\folder';
$pattern = '/\.jpg$/'; //use "//" for all files
$directoryIterator = new RecursiveDirectoryIterator($directory);
$iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
$regexIterator = new RegexIterator($iteratorIterator, $pattern);
foreach ($regexIterator as $file) {
if (is_dir($file)) continue;
echo "$file\n";
}
here is the correct way to search in folders and sub folders
$it = new RecursiveDirectoryIterator(__DIR__);
$findExts = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
if(in_array($ext, $findExts)){
echo $file.PHP_EOL;
}
}
You might want to check out this page about using glob() for a similar search:
http://www.electrictoolbox.com/php-glob-find-files/