0

How to find a file in folders with sub name of the file means the file need to be searched is not fully named but it is a part of the name for example I want to search the file namely 'hello_world_123.png'. I did not enter full name of the file but one or some words only, for example the 'world_' is my input but the code should search the full name of file.

In short the searching may work like operating system's search in file explorer or file manager.

My code as below but I am stuck with matching the name of file in if condition.

$file_name_ = 'recursive-1.php';
$dir = new RecursiveDirectoryIterator( $folder_path, RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($dir);  

foreach($it as $file) {     

    $file_info = pathinfo($file);       

    $patt = '/^(\w+)\.+('.$file_name_.')\.+(\w+)*$/';
    $result = preg_match($patt,strtolower( $file_info['basename'] ),$match );       
    if ( $result ) {    
        echo 'File found';
    } else {
         echo 'File not found';
    }
}
Shailesh Chauhan
  • 559
  • 3
  • 14

2 Answers2

0

To find the file in directory recursively, with or without extension, the following code is helpful. The file name is not complete name (the part of the file name) so the strpos() function will find the position of file name which is partial.

$file_name_ = 'recursive-1.php';

//Splitting the file to get the file name without extension
$file_name_ = explode('.',$file_name_);
$file_name_ = $file_name_[0];

$dir = new RecursiveDirectoryIterator( $folder_path,   RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($dir);  

//looping for all files in given directory
foreach($it as $file) {     

    $file_info = pathinfo($file);       
    //$patt = '/^(\w+)\.+('.$file_name_.')\.+(\w+)*$/';
    //$result = preg_match($patt,strtolower( $file_info['basename'] ),$match );       
    //$result = strpos(strtolower($file_info['filename']),strtolower( $file_name_));    
    $result = stripos($file_info['filename'], $file_name_); 
    if ( $result === flase ) {    
        echo 'File not found';
    } else {
        echo 'File found';
    }
}
Shailesh Chauhan
  • 559
  • 3
  • 14
-2

You should use a preg_match() for that. You can look if the file in the folder has part or the full name of the file you are searching for.

J3Rik0
  • 11
  • 1
  • 3
  • Who and why down vote my answer, that's the solution of the problem, you use preg_match to find text that contains what you want? What is wrong with my answer? – J3Rik0 Sep 06 '16 at 13:20
  • preg_match is already used in the question and doesn't solve the problem alone. – Lætitia Sep 06 '16 at 14:02
  • @Tonin when I answered the question he wasn't using preg_match(), yes it wasn't a complete answer but it was a starting point and I could've expanded on that, personally I don't think jumping and throwing a minus to a comment is a correct way to go, but hey, who am I to judge, btw, have you checked the edits of the question before down voting? – J3Rik0 Sep 10 '16 at 08:31
  • If you make your answer evolve along the question, then I'll have the opportunity to review my vote. As is, your answer is not useful to anyone looking at the question. – Lætitia Sep 10 '16 at 09:56