PHP RecursiveDirectoryIterator and glob() is not working with ñ - php

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

Related

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 !!!

RecursiveDirectoryIterator Show Folder First

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

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!

php file search issue - not returning full path to file

This script will search my server for files starting by a specific string, then return the download links. Search query is defined by "f" GET value.
Now let's say that i have the following files on my server:
/folder/example file number one.zip
/folder/example file number two.zip
/folder/example file number three.zip
If i search for "example file" then the script will return 3 results BUT every download links will be "/folder/example file" instead of the FULL filename (/folder/example file number XXX.zip).
This will also create a bug with the filesize() function at the end of the script, since filesize() will look for the size of "/folder/example file" instead of using the full filename
Can you help me to fix that ?
$request = $_GET['f'];
$adr = $_SERVER['QUERY_STRING'];
$decode = rawurldecode(substr($adr, 2));
echo "Searching for $decode";
// finding the file on the server
$root = $_SERVER['DOCUMENT_ROOT'];
$search = preg_quote(utf8_decode($decode));
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 = array_merge($fileList, $file);
}
return $fileList;
}
$resultatss = rsearch($root, '/.*\/'.$search.'/');
foreach ($resultatss as $resultat) {
$downloadlink = str_replace("$root/", "", $resultat);
$pos = strrpos($downloadlink, '/') + 1;
$encodedownloadlink = substr($downloadlink, 0, $pos) . rawurlencode(substr($downloadlink, $pos));
if (!empty($downloadlink)) {
echo "download link = http://www.mydomain.com/$encodedownloadlink";
$taillekb = filesize($downloadlink) / 1024;
echo "<br>Size: $taillekb KB<br>";
} else {
echo "File not found";
}
}
I was unfamiliar with the RecursiveDirectoryIterator function, so I decided to check it out. Neat stuff! I wrote the following, it seems to do what you want:
Method #1 ...
$path = $_SERVER['DOCUMENT_ROOT'];
$search = rawurldecode($_GET['f']);
$it = new RecursiveDirectoryIterator( $path );
foreach (new RecursiveIteratorIterator($it) as $file){
$pathfile = str_replace($path,'',$file);
if (strpos($pathfile, $search) !== false) {
echo 'file = '. basename($file) .'<br>';
echo 'link = http://www.mydomain.com'. $pathfile .'<br>';
echo 'size = '. round(filesize($file)/1024,2) .' KB<br><br>';
}
}
You run it via http://www.mydomain.com/search.php?f=whatever ... assuming you name the script search.php, this will find any file containing the word whatever.
Method #2 ...
If you want to split this apart (putting the search in a function), here's how that looks:
function rsearch($path, $search) {
$files = array();
$it = new RecursiveDirectoryIterator( $path );
foreach (new RecursiveIteratorIterator($it) as $file){
if (strpos($file, $search) !== false) $files[] = $file;
}
return $files;
}
$path = $_SERVER['DOCUMENT_ROOT'];
$search = rawurldecode($_GET['f']);
$files = rsearch($path, $search);
foreach ($files as $file) {
$pathfile = str_replace($path,'',$file);
if (strpos($pathfile, $search) !== false) {
echo 'file = '. basename($file) .'<br>';
echo 'link = http://www.mydomain.com'. $pathfile .'<br>';
echo 'size = '. round(filesize($file)/1024,2) .' KB<br><br>';
}
}

Categories