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;
}
}
}
Related
I have a top folder named home and nested folders and files inside
I need to insert some data from files and folders into a table
The following (simplified) code works fine if I manually declare parent folder for each level separatelly, i.e. - home/lorem/, home/impsum/, home/ipsum/dolor/ etc
Is there a way to do this automatically for all nested files and folders ?
Actually, I need the path for each of them on each level
$folders = glob("home/*", GLOB_ONLYDIR);
foreach($folders as $el){
//$path = ??;
//do_something_with folder;
}
$files = glob("home/*.txt");
foreach($files as $el){
//$path = ??;
//do_something_with file;
}
PHP has the recursiveIterator suite of classes - of which the recursiveDirectoryIterator is the correct tool for the task at hand.
# Where to start the recursive scan
$dir=__DIR__;
# utility
function isDot( $dir ){
return basename( $dir )=='.' or basename( $dir )=='..';
}
# create new instances of both recursive Iterators
$dirItr=new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::KEY_AS_PATHNAME );
$recItr=new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST );
foreach( $recItr as $obj => $info ) {
# directories
if( $info->isDir() && !isDot( $info->getPathname() ) ){
printf('> Folder=%s<br />',realpath( $info->getPathname() ) );
}
# files
if( $info->isFile() ){
printf('File=%s<br />',$info->getFileName() );
}
}
I would suggest you to use The Finder Component
use Symfony\Component\Finder\Finder;
$finder = new Finder();
// find all files in the home directory
$finder->files()->in('home/*');
// To output their path
foreach ($finder as $file) {
$path = $file->getRelativePathname();
}
I've created custom elements for WPBakery. In the functions.php file, I currently have the following:
add_action( 'vc_before_init', 'vc_before_init_actions' );
function vc_before_init_actions() {
require_once('vc_elements/text-image/init.php' );
require_once('vc_elements/text/init.php' );
}
However, as I build more custom elements, that list will be huge. What I'm looking to do is load all files named init.php in each vc_elements subfolder.
This is my current folder structure:
vc_elements
text-image
init.php
text
init.php
What's the cleanest way to go about this?
You need to use RecursiveDirectoryIterator to scan the folder and get all files which are named as init.php. Below is the code you can use
add_action( 'vc_before_init', 'vc_before_init_actions' );
function vc_before_init_actions() {
$dir = '/full_path_to_vc_elements';
$files = getFiles($dir, 'init.php');
foreach( $files as $file) {
require_once( $file );
}
}
function getFiles($dir, $match) {
$return = array();
$iti = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach($iti as $file){
if ($file->isDir()) {
continue;
}
if(strpos($file , $match) !== false){
$return[] = $file->getPathname();
}
}
return $return;
}
So in future if you add any init.php file inside the folder, it will automatically be picked by getFiles() and included using require.
What I'm looking to do is load all files named init.php in each
vc_elements subfolder.
Assuming you mean only immediate sub-directorys of "vc_elements" you can use GlobIterator with an "*" as a subdirectory wildcard:
$myInitFiles = new GlobIterator('/path/to/vc_elements/*/init.php');
foreach ($myInitFiles as $file) {
require_once( $myInitFiles->key() );
}
unset($myInitFiles); // release object memory for garbage collection
You can obviously convert this to a more general function if required.
Perhaps something like this would work, assuming that directory only contains sub-directories with the elements you need.
$dir = 'path/to/vc_elements';
// Scan directory for its contents and put in array. Remove possiblity of . or ..
$files = array_diff(scandir($dir), array('..', '.'));
foreach ($files as $file) {
$name = '/path/to/vc_elements/' . $file . '/init.php';
include $name;
}
Not sure what the file structor is for your theme but if you have a folder like inc or int make a file called vc-functions.php in that file do something like this.
add_action( 'vc_before_init', 'vc_before_init_actions' );
function vc_before_init_actions() {
require_once('vc_elements/text-image/init.php' );
require_once('vc_elements/text/init.php' );
}
Then in the functions.php
require get_template_directory() . '/inc/vc-functions.php';
I'm looping unto directories and each directory consist of multiple files that yet to be rename. Below is the code
<?php
$path = __DIR__.'/';
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $f ){
$files2 = array_diff(scandir($path.'/'.$f), array('.', '..'));
foreach( $files2 as $f2 ){
rename($path.'/'.$f.'/'.$f2, $path.'/'.$f.'/'.strtolower(str_replace(' ','_',$f2)));
echo 'success<br>';
}
}
above codes return an error of
The system cannot find the file specified. (code: 2)
in each directory, some of the files has the name that consist of special character(s) e.g. Velāyat-e Nūrestān.json.
Any ideas ?
Your code,in the actual state can't work because there are a lot of issues in it.I suggest to use native Directory Iterator to achieve this properly .
You can use:
$root=__DIR__.'/';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));//create a recursive directory iterator
$it->rewind();
while($it->valid())
{
if (
!$it->isDot()//if file basename not in ['.','..']
&&$it->isFile()//and is really a file and not a directory
)
{
rename(str_replace('/','\\',$it->getPathname()),str_replace('/','\\',$it->getPath().'\\'.mb_strtolower(str_replace(' ','_',$it->getBasename()))));//try to rename it
echo "success";
}
$it->next();
}
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 have the following code snippet. I'm trying to list all the files in a directory and make them available for users to download. This script works fine with directories that don't have sub-directories, but if I wanted to get the files in a sub-directory, it doesn't work. It only lists the directory name. I'm not sure why the is_dir is failing on me... I'm a bit baffled on that. I'm sure that there is a better way to list all the files recursively, so I'm open to any suggestions!
function getLinks ($folderName, $folderID) {
$fileArray = array();
foreach (new DirectoryIterator(<some base directory> . $folderName) as $file) {
//if its not "." or ".." continue
if (!$file->isDot()) {
if (is_dir($file)) {
$tempArray = getLinks($file . "/", $folderID);
array_merge($fileArray, $tempArray);
} else {
$fileName = $file->getFilename();
$url = getDownloadLink($folderID, $fileName);
$fileArray[] = $url;
}
}
}
Instead of using DirectoryIterator, you can use RecursiveDirectoryIterator, which provides functionality for iterating over a file structure recursively. Example from documentation:
$path = realpath('/etc');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
echo "$name\n";
}
This prints a list of all files and
directories under $path (including
$path ifself). If you want to omit
directories, remove the
RecursiveIteratorIterator::SELF_FIRST
part.
You should use RecursiveDirectoryIterator, but you might also want to consider using the Finder component from Symfony2. It allows for easy on the fly filtering (by size, date, ..), including dirs or files, excluding dirs or dot-files, etc. Look at the docblocks inside the Finder.php file for instructions.