0

Hello developer friends,

i have a task that described as my question subject. in my case, the directory structure is deep enough and this is the directory sample:

D:\DATA\PROGRAM 11\AREA 01\CAB 001\JS 0100 INSIGHT\2008\01 JANUARY\01 

i have to find image files inside each last child directory. I have to find files with the help from some parameters, some parameter are string that contain in file_name, and some parameter to help system match directory name then searching method can be more accurately. My code still not complete and take long time to complete, it's about 4-5 minutes. please see my code below,

<?php function readdirScandir($dir, $extension) { $files = array(); $root = @scandir($dir, SCANDIR_SORT_NONE); foreach($root as $entry) { if($entry === '.' || $entry === '..') continue; $fullpath = $dir.'/'.$entry; if(is_file($fullpath)) { if (0 === strcasecmp($extension, pathinfo($fullpath, PATHINFO_EXTENSION))) $files[] = $fullpath; continue; } foreach(readdirScandir($fullpath, $extension) as $entry) { if(0 === strcasecmp($extension, pathinfo($entry, PATHINFO_EXTENSION))) { $files[] = $entry; } } } return $files; } $root_dir = getcwd(); //place this before any script you want to calculate time $time_start = microtime(true); // include subdirectories $dirlist = readdirScandir($root_dir, 'tif'); var_dump($dirlist); $time_end = microtime(true); //dividing with 60 will give the execution time in minutes other wise seconds $execution_time = ($time_end - $time_start)/60; //execution time of the script echo '<br><b>Total Execution Time:</b> '.$execution_time.' Mins'; ?> 

I would appreciate every advice and your help. Thanks in advance and nice weekend.

10
  • Take a profiler and profile your code. Commented Nov 19, 2017 at 6:31
  • why you give me minus? i really wanna know the best method first, about time i tcould be estimated later Commented Nov 19, 2017 at 6:35
  • I have not downvoted you. If you want to optimise your code - first profile it. There is no absolute "the best" solution for anything. Commented Nov 19, 2017 at 6:35
  • oh sorry dude it's my fault, yeah the first thing that i need to know is what class that have capability to do it. PHP have some function like opendir(), glob(), scandir() and RecursiveDirectoryIterator class. Commented Nov 19, 2017 at 6:40
  • 1
    @Khazefa How many files/directories are there in total in your start directory? The problem is most likely not PHP but the filesystem trying to read all files/directories. That said it might be wise to check only the directories which are relevant for your search. Please edit your question to include the directory structure, what you are trying to do/search and how you might be able to filter dictionary/files which aren't relevant for your search. Commented Nov 19, 2017 at 10:24

1 Answer 1

1

use the below method to get your files with any extension in current directory and which takes

Total Execution Time: 0.00043816566467285 Mins

You can take a look at glob function which is far better than scandir.

if ( ! function_exists('glob_recursive')) { // Does not support flag GLOB_BRACE function glob_recursive($pattern, $flags = 0) { $files = glob($pattern, $flags); foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); } return $files; } } $time_start = microtime(true); //put your file extension to get all the files from current directory $files = glob_recursive('*.png'); var_dump('<pre>', $files); $time_end = microtime(true); //dividing with 60 will give the execution time in minutes other wise seconds $execution_time = ($time_end - $time_start) / 60; //execution time of the script echo '<br><b>Total Execution Time:</b> ' . $execution_time . ' Mins'; 
Sign up to request clarification or add additional context in comments.

4 Comments

Big thanks mr @Mirza but still lack in processing time, Total Execution Time: 4.8777456680934 Mins, i'm still looking for class that capable doing that in less time..
how big the directories are?
this is the example of directory tree D:\DATA\PROGRAM 11\AREA 01\CAB 001\JS 0100 INSIGHT\2008\01 JANUARY\01
i think that structure is too deep for PHP classes works fastly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.