scandir

PHP RecursiveDirectoryIterator

限于喜欢 提交于 2019-11-28 13:40:10
I want to do a RecursiveDirectoryIterator on a set of folders in a directory, say ./temp and then list the files in each folder according to the name of the folder. For example I have the folders A and B . In A, I have a list of files say, 1.txt , 2.php , 2.pdf , 3.doc , 3.pdf . In B, I have 1.pdf , 1.jpg , 2.png . I want my results to be like this: A => List of files in A B => List of files in B How can this be done? <?php $scan_it = new RecursiveDirectoryIterator("./temp"); foreach(new RecursiveIteratorIterator($scan_it) as $file =>$key) { $filetypes = array("pdf"); $filetype = pathinfo(

PHP scandir recursively

余生长醉 提交于 2019-11-28 09:08:27
问题 I'd like my script to scandir recursively, $files = scandir('/dir'); foreach($files as $file){ if(is_dir($file)){ echo '<li><label class="tree-toggler nav-header"><i class="fa fa-folder-o"></i>'.$file.'</label>'; $subfiles = scandir($rooturl.'/'.$file); foreach($subfiles as $subfile){ // and so on and on and on } echo '<li>'; } else { echo $file.'<br />'; } } I'd like to loop this in a way that for each dir found by scandir, it runs another scandir on the folders that have been found within

php get full path of file in a folder

那年仲夏 提交于 2019-11-28 06:17:40
问题 I have a folder which named data . In this data folder, there are files and folders. In this folders there is files and folders.... Continues like this... I need to get all file path's (include sub directories' files). I have a php code: function dirToArray($dir) { $contents = array(); foreach (scandir($dir) as $node) { if ($node == '.' || $node == '.htaccess' || $node == '..' || $node == '.tmb' || $node == '.quarantine') continue; if (is_dir($dir . '/' . $node)) { $contents[$node] =

PHP: scandir() is too slow

十年热恋 提交于 2019-11-27 21:38:10
I have to make a function that lists all subfolders into a folder. I have a no-file filter, but the function uses scandir() for listing. That makes the application very slow. Is there an alternative of scandir(), even a not native php function? Thanks in advance! You can use readdir which may be faster, something like this: function readDirectory($Directory,$Recursive = true) { if(is_dir($Directory) === false) { return false; } try { $Resource = opendir($Directory); $Found = array(); while(false !== ($Item = readdir($Resource))) { if($Item == "." || $Item == "..") { continue; } if($Recursive =

Include JUST files in scandir array?

こ雲淡風輕ζ 提交于 2019-11-27 21:17:00
I have an array I'm getting back from scandir, but it contains "." and ".." and I don't want it to. My code: $indir = scandir('../pages'); $fileextensions = array(".", "php", "html", "htm", "shtml"); $replaceextensions = str_replace($fileextensions, "", $indir); I am doing a string replace on the file extensions, thus causing [0] and [1] to appear empty, but they are "." and ".." array(4) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(4) "test" [3]=> string(4) "home" } How would I remove the "." and ".." from the array? You can use array_filter . $indir = array_filter(scandir('../pages')

Exclude hidden files from scandir

不打扰是莪最后的温柔 提交于 2019-11-27 14:56:34
I am using the following code to get a list of images in a directory: $files = scandir($imagepath); but $files also includes hidden files. How can I exclude them? mario On Unix, you can use preg_grep to filter out filenames that start with a dot: $files = preg_grep('/^([^.])/', scandir($imagepath)); I tend to use DirectoryIterator for things like this which provides a simple method for ignoring dot files: $path = '/your/path'; foreach (new DirectoryIterator($path) as $fileInfo) { if($fileInfo->isDot()) continue; $file = $path.$fileInfo->getFilename(); } function nothidden($path) { $files =

How to check a path is a file or folder in PHP

主宰稳场 提交于 2019-11-26 23:37:33
问题 I use scandir() to search files recursively. But if the file path directs to a file not a folder, there will be a warning. How can I check the path whether it directs a file or folder? enter code here <?php $path = "C:\\test_folder\\folder2\\folder4"; $sub_folder = scandir($path); $num = count($sub_folder); for ($i = 2; $i < $num; $i++) { ...//if $sub_folder[$i] is a file but a folder, there will be a warning. How can I check $sub_folder[$i] before use it? } ?> Thanks! 回答1: Have a look into

Include JUST files in scandir array?

元气小坏坏 提交于 2019-11-26 23:03:04
问题 I have an array I'm getting back from scandir, but it contains "." and ".." and I don't want it to. My code: $indir = scandir('../pages'); $fileextensions = array(".", "php", "html", "htm", "shtml"); $replaceextensions = str_replace($fileextensions, "", $indir); I am doing a string replace on the file extensions, thus causing [0] and [1] to appear empty, but they are "." and ".." array(4) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(4) "test" [3]=> string(4) "home" } How would I

PHP: scandir() is too slow

青春壹個敷衍的年華 提交于 2019-11-26 20:44:04
问题 I have to make a function that lists all subfolders into a folder. I have a no-file filter, but the function uses scandir() for listing. That makes the application very slow. Is there an alternative of scandir(), even a not native php function? Thanks in advance! 回答1: You can use readdir which may be faster, something like this: function readDirectory($Directory,$Recursive = true) { if(is_dir($Directory) === false) { return false; } try { $Resource = opendir($Directory); $Found = array();

scandir() to sort by date modified

这一生的挚爱 提交于 2019-11-26 20:36:44
I'm trying to make scandir(); function go beyond its written limits, I need more than the alpha sorting it currently supports. I need to sort the scandir(); results to be sorted by modification date. I've tried a few solutions I found here and some other solutions from different websites, but none worked for me, so I think it's reasonable for me to post here. What I've tried so far is this: function scan_dir($dir) { $files_array = scandir($dir); $img_array = array(); $img_dsort = array(); $final_array = array(); foreach($files_array as $file) { if(($file != ".") && ($file != "..") && ($file !=