RecursiveDirectoryIterator Show Folder First - php

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);

Related

PHP RecursiveDirectoryIterator and glob() is not working with ñ

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();
}
}
}

How do I delete all UNUSED images from my uploads directory if do not saved in Database in PHP

I use laravel 4.2
I have more than 1,000 images in the project; these images have been occupied by host users after multiple uploads. Please edit the sample instance in such a way that it will erase all images except images that have both the name and the file stored.
public function del_image()
{
$scan = scandir('uploads/evidence');
foreach($scan as $file)
{
if (!is_dir($file))
{
$list = DB::table('evidence')
->where('profile_img',$file)
->select('profile_img')->get();
echo '<pre>';print_r($list); echo '</pre>';
}
}
//echo $list."<br>";
//echo '<pre>';print_r($list); echo '</pre>';
}
Thanks.
$scan = scandir('uploads/evidence');
$files = DB::table('evidence')->pluck('profile_img');
$protectTheseImages = [];
foreach($files as $file) {
$protectTheseImages[] = $file;
}
$diff = array_diff($scan, $protectTheseImages);
foreach($diff as $file) {
if (!is_dir($file)) {
unlink($file);
}
}
I think You should use this code.
$scan = scandir('uploads/evidence');
$files = DB::table('evidence')->pluck('profile_img');
$protectTheseImages = [];
foreach((array)$files as $file) {
$protectTheseImages[] = $file;
}
//dd($protectTheseImages);
$diff = array_diff($scan, $protectTheseImages);
foreach($diff as $file) {
if (!is_dir($file)) {
echo $file."<br>";
$image_path = '/folder_path/uploads/evidence/'.$file;
unlink($image_path);
}
}
Hope This Code Helps you.
this code i updated to :
$scan = scandir('uploads/evidence');
$files = DB::table('evidence')->pluck('profile_img');
$protectTheseImages = [];
foreach((array)$files as $file) {
$protectTheseImages[] = $file;
}
//dd($protectTheseImages);
$diff = array_diff($scan, $protectTheseImages);
foreach($diff as $file) {
if (!is_dir($file)) {
echo $file."<br>";
//unlink($file);
}
}
but, line echo $file."<br>"; just show all images in directory !!!

Scandir scan all sub folder infinetly

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>";

PHP recursive directory search using glob for multiple file types

I have a bunch of folders with these file types spread across them .mp4,.flv,.wmv,.mov
/video/o/y/oyr800/clips/1.flv
/video/p/y/pyr800/clips/1.wmv
/video/q/y/qyr800/clips/1.mp4
/video/51/51.mov
/video/52/52.flv
I tried using this function to list the paths and filenames but it gives me a blank return:
print_r(rglob('{*.mp4,*.flv,*.wmv,*.mov}',GLOBAL_BRACE));
function rglob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}
Not sure what is wrong.
Ended up using this:
print_r(rsearch("C:\wamp\www","/^.*\.(mp4|flv|wmv|mov)$/"));
function rsearch($folder, $pattern) {
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
$fileList = array();
foreach($files as $file) {
$fileList[] = $file[0];
}
return $fileList;
}
Works great!

DirectoryIterator still showing 3 files when the forder is empty

I have following code to count number of files in a folder using php
$x=0;
$filepath=$uplaod_drive."Workspace/12345";
$dir = new DirectoryIterator($filepath);
foreach($dir as $file ){
$x++;
}
But even if the folder is empty it show 3 files are there and if the folder has 8 files it show 11 files.
I would be very thankful if someone could explain this ..Thanks.
if you want to count only regular files:
$x=0;
$filepath=$uplaod_drive."Workspace/12345";
$dir = new DirectoryIterator($filepath);
foreach($dir as $file ){
if ($file->isFile()) $x++;
}
or if you want to skip the directories:
$x=0;
$filepath=$uplaod_drive."Workspace/12345";
$dir = new DirectoryIterator($filepath);
foreach($dir as $file ){
if (!$file->isDir()) $x++;
}
or if you want to skip the dot files:
$x=0;
$filepath=$uplaod_drive."Workspace/12345";
$dir = new DirectoryIterator($filepath);
foreach($dir as $file ){
if (!$file->isDot()) $x++;
}
The DirectoryIterator is counting the current directory (denoted by '.') and the previous directory (denoted by '..') as $files. Debug your code like so.
$x=0;
$filepath='C:\xampp\htdocs\\';
$dir = new DirectoryIterator($filepath);
foreach($dir as $file ){
echo $file . "<br/>";
$x++;
}
echo $x;
Then as mentioned in the comment above by #Prix you can skip if $file->isDot(), and if you dont want to count the directories then also skip if not $file->isFile().
$x=0;
$filepath='C:\xampp\htdocs\\';
$dir = new DirectoryIterator($filepath);
foreach($dir as $file ){
if ($file->isDot() || !$file->isFile()) continue;
//echo $file . "<br/>";
$x++;
}
echo $x;

Categories