Quantcast
Channel: Сообщество PHP DevelStudio - среда разработки программ
Viewing all articles
Browse latest Browse all 6336

Умный поиск файлов в папке, и в подпапках.

$
0
0
Что то мне не иметься раз идея появилась :a1:

Эта функция ищет по 4'ем настройкам
Скрытый текст:
----------------------------------------------------------------------------------------------------------
SEARCH_PATH - Поиск все что в папке.
На выходе массив
Код:

array(
    'Path' => array(),
    'Files' => array()
)

----------------------------------------------------------------------------------------------------------
SEARCH_PATH_EXT - Поиск все что в папке с заданным расширением.
На выходе массив
Код:

array(
    'Path' => array(),
    'Files' => array()
)

----------------------------------------------------------------------------------------------------------
SEARCH_ALL_PATH - Поиск все что в папке и подпапках.
На выходе массив
Код:

array(
    'Path' => array(array(
          'Path' => array(),
          'Files' => array()
      )),
    'Files' => array()
)

----------------------------------------------------------------------------------------------------------
SEARCH_ALL_PATH_EXT - Поиск все что в папке и подпапках, с заданным расширением.
На выходе массив
Код:

array(
    'Path' => array(array(
          'Path' => array(),
          'Files' => array()
      )),
    'Files' => array()
)



:a2:


Описания функции
Скрытый текст:
SearchPath( string Path, const int opt = SEARCH_PATH, mixed ext = null)

string Path - Путь до папок
const int opt - Опции SEARCH_PATH, SEARCH_PATH_EXT, SEARCH_ALL_PATH, SEARCH_ALL_PATH_EXT
По умолчанию - SEARCH_PATH = Поиск все что в папке.


mixed ext - Поиск с заданным расширением, тока для опций SEARCH_PATH_EXT, SEARCH_ALL_PATH_EXT
Во всех других этот параметр будет игнорироваться.
По умолчанию - NULL
Так же может иметь массив с расширениями, которые должны остаться



Примеры использования
Скрытый текст:
PHP код:

$dir 'C:\3DPHP';

$Path SearchPath($dirSEARCH_PATH);
pre($Path);

$Path SearchPath($dirSEARCH_ALL_PATH);
pre($Path);

$ext 'cpp'# Или $ext = array('cpp', 'log');

$Path SearchPath($dirSEARCH_PATH_EXT$ext);
pre($Path);

$Path SearchPath($dirSEARCH_ALL_PATH_EXT$ext);
pre($Path); 




Функция выглядит громозка, но самая быстрая которую вы можете найти за даром!.
Скрытый текст:

PHP код:

define('SEARCH_PATH'0);
define('SEARCH_PATH_EXT'2);

define('SEARCH_ALL_PATH'1);
define('SEARCH_ALL_PATH_EXT'3);

function 
ExtFiles($str) {
    if(
false !== ($i strrpos($str'.')))
        return 
strtolower(substr($str$i+1));
    return 
null;
}

function 
SearchPath($Path$opt SEARCH_PATH$ext null) {
    
$Path preg_replace('|([/]+)|s''/',  str_replace('\\''/'$Path.'/'));
    if(!
is_dir($Path)) return NULL;
    
$dir dir($Path);
    
$List = array();

    if(
$ext !== null)
        if(
is_array($ext)) {
            foreach(
$ext as $vz)
                
$extpc[strtolower($vz)] = true;
        } else
            
$ext strtolower($ext);

    if(
$dir != false and $dir != null)
        while (
false !== ($v $dir->read())) {
            if(
$v != '.' and $v != '..')
                switch(
$opt) {
                    case 
SEARCH_ALL_PATH:
                        
is_file($Path.$v) ? $List['Files'][$Path][] = $Path.$v $List['Path'][$Path.$v] = SearchPath($Path.$v$opt);
                    break;
                    case 
SEARCH_PATH:
                        
$List[is_dir($Path.$v) ? 'Path' 'Files'][] = $Path.$v;
                    break;
                    
                    case 
SEARCH_ALL_PATH_EXT:
                        if(
is_dir($Path.$v))
                            
$List['Path'][$Path.$v] = SearchPath($Path.$vSEARCH_PATH_EXT$ext);
                        else {
                            
$extI ExtFiles($Path.$v);
                            if((
$extI !== null) and ($ext !== null)) {
                                if(
is_array($ext)) {
                                    if(
$extpc[$extI])
                                        
$List['Files'][] = $Path.$v;
                                } else
                                    if(
$ext == $extI)
                                        
$List['Files'][] = $Path.$v;
                            }
                        }
                    break;
                    case 
SEARCH_PATH_EXT:
                        if(
is_dir($Path.$v))
                            
$List['Path'][] = $Path.$v;
                        else {
                            
$extI ExtFiles($Path.$v);
                            if((
$extI !== null) and ($ext !== null)) {
                                if(
is_array($ext)) {
                                    if(
$extpc[$extI])
                                        
$List['Files'][] = $Path.$v;
                                } else
                                    if(
$ext == $extI)
                                        
$List['Files'][] = $Path.$v;
                            }
                        }
                    break;
                }
            unset(
$v);
        }
    unset(
$Path$opt$dir$ext$extpc);
    return 
$List;





Пишите что в ней можно доработать.




Сделал ТЕСТ - Файлов: 14 917; папок: 349
За 9.45201802254 сек. перебрал с флагом SEARCH_ALL_PATH
PHP код:

$time microtime(true);
$Path SearchPath('D:\php-5.3.8'SEARCH_ALL_PATH);
$time = (microtime(true) - $time);
pre($time); 


Viewing all articles
Browse latest Browse all 6336

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>