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>";
}
Related
Pls sir, how can I loop through a directory and get the sub-directory name and all the files names so that I can generate a directory try, am trying to build a file manager in php.
I have tried:
$dir = new DirectoryIterator(dirname(FILE));
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
var_dump($fileinfo->getFilename());
}
}
You can scan a directory and display the name of files in it using the PHP Code below
<?php
$dir = 'dir/';
$files = scandir($dir);
$totFiles = sizeof($files);
for($i=2;$i<$totFiles;$i++){
$name = explode(".",$files[$i]);
echo "
$name[0]
";
}
?>
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.
I'm using RecursiveDirectoryIterator to scan for all files and folders within a given root dir. This works fine, but I'd like to keep track of all of the unique directories in that list, so I'm not sure that RecursiveDirectoryIterator is the way to go.
I have a directory structure of
-a
->b
->c
-one
->two
->three
Here is my code:
<?php
function test($dir){
$in_dir = 'none';
$currdir = 'none';
$thisdir = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($thisdir, RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $object){
//if this is a directory... find out which one it is.
if($object->isDir()){
//figure out if we have changed directories...
$currdir = realpath($object->getPath());
if(strpos($currdir, '.') == false){
$test = strcmp($currdir, $prevdir);
if($test){
echo "current dir changing: ", $currdir, "\n";
$prevdir = $currdir;
}
}
}
}
}
test('fold');
?>
What I get from that is the following:
current dir changing: /Users/<usr>/Desktop/test/fold
current dir changing: /Users/<usr>/Desktop/test/fold/a
current dir changing: /Users/<usr>/Desktop/test/fold/a/b
current dir changing: /Users/<usr>/Desktop/test/fold
current dir changing: /Users/<usr>/Desktop/test/fold/one
current dir changing: /Users/<usr>/Desktop/test/fold/one/two
...But I only want the unique directories.
It's perhaps the method of object comparison in the loop that returns duplicates, as the iterator moves up and down the directory tree as it parses through folders.
The following worked for me. I also use array_unique() confirm no dupes as a redundancy.
$dirArray = []; // the array to store dirs
$path = realpath('/some/folder/location');
$objects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST
);
// loop through all objects and store names in dirArray[]
foreach($objects as $name => $object){
if ($object->isDir()) {
$dirArray[] = $name;
}
}
// make sure there are no dupes
$result = array_unique($dirArray);
// print array out
print_r($result);
I am using this code to get list of sub directories.
<?php
$path = '/www/sites/';
$directory_iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME);
$iterator = new RecursiveIteratorIterator($directory_iterator,
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach ($iterator as $file => $info) {
if ($info->isDir()) {
echo $file . "\n";
}
}
?>
But it is returning me something like this
/www/sites/000/4e7
/www/sites/000/4e7/.
/www/sites/000/4e7/..
/www/sites/000/4e7/mydomains.com
/www/sites/000/4e7/mydomains.com/.
/www/sites/000/4e7/mydomains.com/..
/www/sites/000/4e7/mydomains.com/web
/www/sites/000/4e7/mydomains.com/web/.
/www/sites/000/4e7/mydomains.com/web/..
/www/sites/000/4e7/mydomains.com/web/cgi-bin
/www/sites/000/4e7/mydomains.com/web/cgi-bin/.
/www/sites/000/4e7/mydomains.com/web/cgi-bin/..
/www/sites/000/4e7/mydomains.com/web/Resources
/www/sites/000/4e7/mydomains.com/web/Resources/.
I specified root path as /www/sites
What I only need in the above output is
/www/sites/000/4e7/mydomains.com
Rest are useless to me. It means I only want to crawl up to 3 sub directories from specified path. I have thousands of thousands of sub directories that's why I want to modify current code as it is taking too much time
Thank you in advance.
Use directoryiterator::isdot(), recursiveiteratoriterator::getdepth(), eg:
<?php
$path = '/www/sites/';
$directory_iterator = new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::KEY_AS_PATHNAME);
$iterator = new RecursiveIteratorIterator($directory_iterator,RecursiveIteratorIterator::SELF_FIRST,RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach ($iterator as $file => $info) {
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Restrict to 3 levels only and ignore non-directory and dot files
if ($info->getDepth() >= 3 || !$info->isDir() || $info->isDot())
continue;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo $file . "\n";
}
Output
/www/sites/000
/www/sites/000/4e7
/www/sites/000/4e7/mydomains.com
I want to get the main folder with no subfolders with the following code:
$dir = new RecursiveDirectoryIterator($admin['data_folder_main'], FilesystemIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
$it->setMaxDepth(1);
foreach($it AS $fileinfo) {
if($fileinfo->isDir() AND !$it->isDot()) {
echo $it->getSubPathName().'<br>';
}
}
As it is right now, this code prints
main-folder
main-folder/subfolder
main-folder/subfolder
Does RecursiveDirectoryIterator or RecursiveIteratorIterator have a function that says that "this is the main folder"? Or do I have to use some workaround code? If yes, how will that code look like?
Nevermind! I fixed it like this:
$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($admin['data_folder_main']), FilesystemIterator::SKIP_DOTS, RecursiveIteratorIterator::SELF_FIRST);
foreach($dir as $name => $object) {
if($dir->isDir() AND !$dir->isDot()) {
echo basename($name);
}
}