<?php
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('./'),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) {
if($file->isDir()) {
echo "\n" . strtoupper($file->getRealpath()), PHP_EOL;
}
}
?>
The above is what I have so far. I am looking to go through dirs and also sub-dirs and see which ones are write-able.
Have you tried using the isWritable() method?
<?php
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('./'),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) {
if($file->isDir()) {
echo "\n" . strtoupper($file->getRealpath()), PHP_EOL;
if($file->isWritable()) {
echo "directory is writable\n";
}
}
}
?>
http://www.php.net/manual/en/splfileinfo.iswritable.php
Related
here is my path
c:\Customers\NCR\Las Piñas
and im trying to get all the csv files.
I did the code from here (see top answer)
php glob - scan in subfolders for a file
and also did a mixed recursive and glob
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$html = "";
foreach ($iterator as $folder) {
if ($folder->isDir()) {
$all_files = glob($folder->getRealpath() . "/*.csv");
foreach ($all_files as $filename) {
$html .= $filename . PHP_EOL;
}
}
}
echo $html;
and still cant read the csv inside this folder
so far this is what i came of
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$csvs = [];
foreach ($iterator as $folder) {
if (!$folder->isDir()) {
$ext = pathinfo($folder->getRealpath(), PATHINFO_EXTENSION);
if (strtolower($ext) == 'csv') {
$csvs[] = $folder->getRealpath();
}
}
}
I'm using RecursiveDirectoryIterator to show files from a path:
$pasta = $_SERVER["DOCUMENT_ROOT"]."/files/";
$dir = new RecursiveDirectoryIterator($pasta, FilesystemIterator::SKIP_DOTS);
$dir = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
$dir->setMaxDepth(1);
foreach ($dir as $fileinfo) {
echo $fileinfo->getFilename()."<br/>";
}
This show all files and folder in Alphabetical order, Is there a way to show first folders than files in Alphabetical order?
Using the isDir method to separate directives from files.
$pasta = $_SERVER["DOCUMENT_ROOT"]."/files/";
$dir = new RecursiveDirectoryIterator($pasta, FilesystemIterator::SKIP_DOTS);
$dir = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
$dir->setMaxDepth(1);
$dirs = [];
$files = [];
foreach ($dir as $fileinfo) {
if($fileinfo->isDir())
$dirs[] = $fileinfo->getFilename();
else
$files[] = $fileinfo->getFilename();
}
$result = array_merge($dirs,$files);
echo "<pre>";
print_r($result);
echo implode('<br>', $result);
I have a large piece of code which does a lot of things with folder, files names etc. However, I seemed to have hit an interesting issue.
I am scanning a directory ./wp-content/uploads/webvideos/
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); foreach($iterator as $file) echo $file . "<br />";
$iterator = $files;
$files = array();
$webdir = site_url()."/wp-content/uploads/webvideos/";
This gives me the first level in the array. Is there an easy way to get it to scan the 1st, 2rd, 3rd etc.
New code
$path = "./wp-content/uploads/webvideos/";
$webdir = site_url()."/wp-content/uploads/webvideos/";
$post_title = html_entity_decode(get_the_title());
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
$files = array();
$notAllowedExtension = array('jpg', 'mp3', 'vtr');
foreach($iterator as $file){
if(!in_array($file->getExtension(), $notAllowedExtension) && !$file->isDir())
$files[filemtime($file->getPathname())] = $file->getPathname();
}
ksort($files);
echo "<pre>" . print_r($files, true) . "</pre>";
So I managed to get all files in my directory using this:
$dir = PUBLIC_DIRECTORY . '/' . $login_session . '/';
$files = array();
$DirectoryIterator = new RecursiveDirectoryIterator($dir);
$IteratorIterator = new RecursiveIteratorIterator($DirectoryIterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($IteratorIterator as $file) {
$path = $file;
if ($file->isFile())
$files[] = realpath($path);
}
which resulted to this:
C:\xampp\htdocs\fms\public\kei\docx\FMS.docx
C:\xampp\htdocs\fms\public\kei\jpg\1.jpg
Now i used the code below which should move the files from inside their directory to C:\xampp\htdocs\fms\public\kei\Bin\ when aged
foreach($files as $f) {
if (filemtime($f) < time() - 86400) {
rename($f, realpath($dir . "/Bin/" . basename($f));
}
}
but nothing happens for some reasons. I tried different things to get the filename like...
echo realpath($dir . "/Bin/") . basename($f);
but this code shows
C:\xampp\htdocs\fms\public\kei\BinFMS.docx
C:\xampp\htdocs\fms\public\kei\Bin1.jpg
I managed to fixed it by doing this
$bin = "\Bin\";
and this rename($f, realpath($dir) . $bin . basename($f));
I have a folder structure like this:
/articles
.index.php
.second.php
.third.php
.fourth.php
If I'm writing my code in second.php, how can I scan the current folder(articles)?
Thanks
$files = glob(dirname(__FILE__) . "/*.php");
http://php.net/manual/en/function.glob.php
foreach (scandir('.') as $file)
echo $file . "\n";
From the PHP manual:
$dir = new DirectoryIterator(dirname($path));
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
var_dump($fileinfo->getFilename());
}
}
<?php
$path = new DirectoryIterator('/articles');
foreach ($path as $file) {
echo $file->getFilename() . "\t";
echo $file->getSize() . "\t";
echo $file->getOwner() . "\t";
echo $file->getMTime() . "\n";
}
?>
From The Standard PHP Library (SPL)
It depends on what you mean by 'scan' I'm assuming you want to do something like this:
$dir_handle = opendir(".");
while($file = readdir($dir_handle)){
//do stuff with $file
}
try this
$dir = glob(dirname(__FILE__));
$directory = array_diff(scandir($dir[0]), array('..', '.'));
print_r($directory);
Scan current folder
$zip = new ZipArchive();
$x = $zip->open($filepath);
if ($x === true) {
$zip->extractTo($uploadPath); // place in the directory
$zip->close();
$fileArray = scandir($uploadPath);
unlink($filepath);
}
foreach ($fileArray as $file) {
if ('.' === $file || '..' === $file)
continue;
if (!is_dir("$file")){
//do stuff with $file
}
}
List all images inside a folder
$dir = glob(dirname(__FILE__));
$path = $dir[0].'\\images';
$imagePaths = array_diff( scandir( $path ), array('.', '..', 'Thumbs.db'));
?>
<ul style="overflow-y: auto; max-height: 80vh;">
<?php
foreach($imagePaths as $imagePath)
{
?>
<li><?php echo '<img class="pagina" src="images/'.$imagePath.'" />'; ?></li>
<?php
}
?>
</ul>