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>";
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);
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));
<?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
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>';
}
}