The Identification Number and Password fields need to be filled in. Moreover, you cannot send messages devoid of contents Adding further data is not allowed because this topic has been closed Print You provided no password: you have to write it in the textfield beside the command delete for the topic you want to delete Are you sure you want to delete this comment? You did not cast a vote. You need to cast a vote by selecting a radio button. Impossible to proceed Provide the project number Insufficient form parameters: you forgot to fill in some field/s
 
member picmember pic
 
Snippets of A
 
info Below you can find the text of the snippet you want to read, and the list of the other snippets by this author if available.
What are snippets?
Share on MySpace

PHP Find File In Directory, Scan Subdirectories. Find Array Value In Matrix.

Snippet: Identification Number »   Snippet: Inclusion syntax »
Visitors: 15,898 Tagged by its author as: Programming Php Characters (in origin): 20,270 (pages: ~ 7)
Author: Em@il Permalink Cast your vote for this topic Printable version
OBJECTIVE: implement in Php functions to find files in a directory or items (values) in an array no matter how branched the array is or how many subdirectories the chosen root directory has and how irregularly arranged. Find after full file name or full array value, or after substrings, or using regular expressions.  
 
About five functions belonging to this family can be found on Php: Scan A Directory Or An Array Of Arrays (Matrix, Numerical Or Associative).  
Find a file name scanning a branched directory, stop when found.  
function findFile($node, $value, $limit=0, $separator="\\"){
//validate:
if(!is_dir($node) || !isset($value) || $value===""){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$stack=array( array($node, $slashes) );
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]); 
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else if($it === $value){return $path;};
		};
	}
return false;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then the string file name to locate inclusive of extension if present.  
Then an optional number representing the nested folders depth, zero meaning scan all subfolders.  
A last argument is optional and represents the directory separator implemented on your operative system, defaults to the Windows backward slash.  
 
RETURNS: null in case of wrong inputs, boolean false if no file is found or the string path to the found file, inclusive of the file name.  
 
CAVEATS: case sensitive.  
Same as above but case insensitive:  
function findFilei($node, $value, $limit=0, $separator="\\"){
//validate:
if(!is_dir($node) || !isset($value) || $value===""){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$stack=array( array($node, $slashes) ); $value=strtolower($value);
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]);
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else if(strtolower($it) === $value){return $path;};
		};
	}
return false;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
Find a file name scanning a branched directory, report all found instances.  
function findFiles($node, $value, $limit=0, $separator="\\"){
//validate:
if(!is_dir($node) || !isset($value) || $value===""){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$output=array(); $stack=array( array($node, $slashes) );
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]);
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else if($it === $value){$output[]=$path;};
		};
	}
return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then the string file name to locate inclusive of extension if present.  
Then an optional number representing the nested folders depth, zero meaning scan all subfolders.  
A last argument is optional and represents the directory separator implemented on your operative system, defaults to the Windows backward slash.  
 
RETURNS: null in case of wrong inputs, an empty array if no file is found or an array whose each item is a string path to the found file(s), inclusive of the file name(s).  
 
CAVEATS: case sensitive.  
Same as above but case insensitive:  
function findFilesi($node, $value, $limit=0, $separator="\\"){
//validate:
if(!is_dir($node) || !isset($value) || $value===""){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$output=array(); $stack=array( array($node, $slashes) ); $value=strtolower($value);
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]);
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else if(strtolower($it) === $value){$output[]=$path;};
		};
	}
return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
Find a file name given a substring of the name (partial name, that is - such as also just the extension) scanning a branched directory, report all found instances.  
function findFiless($node, $value, $limit=0, $separator="\\"){
//validate:
if(!is_dir($node) || !isset($value) || $value===""){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$output=array(); $stack=array( array($node, $slashes) ); $value=strtolower($value);
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]);
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else if(strpos(strtolower($it), $value)  !== false){$output[]=$path;};
		};
	}
return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then the string file name to locate inclusive of extension if present.  
Then an optional number representing the nested folders depth, zero meaning scan all subfolders.  
A last argument is optional and represents the directory separator implemented on your operative system, defaults to the Windows backward slash.  
 
RETURNS: null in case of wrong inputs, an empty array if no file is found or an array whose each item is a string path to the found file(s), inclusive of the file name(s).  
 
CAVEATS: case sensitive.  
Find a file name given a regular expression (so it may allow also partial name, that is - such as also just the extension) scanning a branched directory, report all found instances.  
function findFilesr($node, $regexp, $limit=0, $separator="\\"){
//validate:
if(!is_dir($node) || !is_string($regexp)){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$output=array(); $stack=array( array($node, $slashes) );
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]);
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else if(preg_match($regexp,$it)){$output[]=$path;};
		};
	}
return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then a regular expression in between quotes, inclusive of the leading and trailing forward slashes; after the trailing ending slash you can add a i flag to make it case insensitive if you prefer so.  
Then an optional number representing the nested folders depth, zero meaning scan all subfolders.  
A last argument is optional and represents the directory separator implemented on your operative system, defaults to the Windows backward slash.  
 
RETURNS: null in case of wrong inputs, an empty array if no file is found or an array whose each item is a string path to the found file(s), inclusive of the file name(s).  
Find a file name given a regular expression (so it may allow also partial name, that is - such as also just the extension) scanning a branched directory, stop when found.  
function findFiler($node, $regexp, $limit=0, $separator="\\"){
//validate:
if(!is_dir($node) || !is_string($regexp)){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$stack=array( array($node, $slashes) );
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]);
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else if(preg_match($regexp,$it)){return $path;};
		};
	}
return false;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then a regular expression in between quotes, inclusive of the leading and trailing forward slashes; after the trailing ending slash you can add a i flag to make it case insensitive if you prefer so.  
Then an optional number representing the nested folders depth, zero meaning scan all subfolders.  
A last argument is optional and represents the directory separator implemented on your operative system, defaults to the Windows backward slash.  
 
RETURNS: null in case of wrong inputs, boolean false if no file is found or the string path to the found file, inclusive of the file name.  
Remember Php: Scan A Directory Or An Array Of Arrays (Matrix, Numerical Or Associative).  
 
Next function:  
Find a file name scanning a branched numerically indexed array, stop when found.  
function find($node, $value){
if(!$node || !is_array($node) || (!isset($value) && $value!==null)){return null;};
$stack=array( array($node, sizeof($node), array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
		$nextObjectIndex=( sizeof($stack[$currentStackIndex][0]) - $stack[$currentStackIndex][1] );
		--$stack[$currentStackIndex][1];
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectIndex], sizeof($stack[$currentStackIndex][0][$nextObjectIndex]), array_merge($stack[$currentStackIndex][2], array($nextObjectIndex)))
			);
		continue;};
			if( $stack[$currentStackIndex][0] === $value ){
				return array($stack[$currentStackIndex][0], $stack[$currentStackIndex][2]);
			};
		array_pop($stack);
	}
	return false;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then the string value to locate inclusive of extension if present.  
 
RETURNS: null in case of wrong inputs, boolean false if no such value is found or an array of two items. The first is the item found at the end of each branch, and the second item is an array again, whose each entry is a number: that is the map of all the traversed indexes to get at the reported item.  
 
CAVEATS: case sensitive.  
Find a file name scanning a branched numerically indexed array, report all found items.  
function findAll($node, $value){
if(!$node || !is_array($node) || (!isset($value) && $value!==null)){return null;};
$output=array(); $stack=array( array($node, sizeof($node), array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
		$nextObjectIndex=( sizeof($stack[$currentStackIndex][0]) - $stack[$currentStackIndex][1] );
		--$stack[$currentStackIndex][1];
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectIndex], sizeof($stack[$currentStackIndex][0][$nextObjectIndex]), array_merge($stack[$currentStackIndex][2], array($nextObjectIndex)))
			);
		continue;};
			if( $stack[$currentStackIndex][0] === $value ){
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][2]);
			};
		array_pop($stack);
	}
	return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then the string value to locate inclusive of extension if present.  
 
RETURNS: null in case of wrong inputs, an empty array if no such value is found or an array of two items. The first is the item found at the end of each branch, and the second item is an array again, whose each entry is a number: that is the map of all the traversed indexes to get at the reported item.  
 
CAVEATS: case sensitive.  
Find a file name scanning a branched associative array, stop when found.  
function associativeFind($node, $value){
if(!$node || !is_array($node) || (!isset($value) && $value!==null)){return null;};
$stack=array( array($node, 0, 0, array(), array(), array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if( !$stack[$currentStackIndex][2] && is_array($stack[$currentStackIndex][0]) ){
		$stack[$currentStackIndex][3]=array_keys($stack[$currentStackIndex][0]);
		$stack[$currentStackIndex][1]=sizeof($stack[$currentStackIndex][3]);/*decreasing length*/
		$stack[$currentStackIndex][2]=sizeof($stack[$currentStackIndex][3]);/*fixed length*/
		};
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
		$nextObjectIndex=( $stack[$currentStackIndex][2] - $stack[$currentStackIndex][1] );
		$nextObjectAssociativeIndex=$stack[$currentStackIndex][3][$nextObjectIndex];
		--$stack[$currentStackIndex][1];
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectAssociativeIndex], 0, 0, array(), array_merge($stack[$currentStackIndex][4], array($nextObjectAssociativeIndex)), array_merge($stack[$currentStackIndex][5], array($nextObjectIndex))
				)
			);
		continue;};
			if( $stack[$currentStackIndex][0] === $value ){
				return array($stack[$currentStackIndex][0], $stack[$currentStackIndex][4], $stack[$currentStackIndex][5]);
			};
		array_pop($stack);
	}
	return false;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then the string value to locate inclusive of extension if present.  
 
RETURNS: null in case of wrong inputs, boolean false if no such value is found or an array of two items. The first is the item found at the end of each branch, and the second item is an array again, whose each entry is a number: that is the map of all the traversed indexes to get at the reported item.  
 
CAVEATS: case sensitive.  
Find a file name scanning a branched associative array, report all found items.  
function associativeFindAll($node, $value){
if(!$node || !is_array($node) || (!isset($value) && $value!==null)){return null;};
$output=array();
$stack=array( array($node, 0, 0, array(), array(), array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if( !$stack[$currentStackIndex][2] && is_array($stack[$currentStackIndex][0]) ){
		$stack[$currentStackIndex][3]=array_keys($stack[$currentStackIndex][0]);
		$stack[$currentStackIndex][1]=sizeof($stack[$currentStackIndex][3]);/*decreasing length*/
		$stack[$currentStackIndex][2]=sizeof($stack[$currentStackIndex][3]);/*fixed length*/
		};
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
		$nextObjectIndex=( $stack[$currentStackIndex][2] - $stack[$currentStackIndex][1] );
		$nextObjectAssociativeIndex=$stack[$currentStackIndex][3][$nextObjectIndex];
		--$stack[$currentStackIndex][1];
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectAssociativeIndex], 0, 0, array(), array_merge($stack[$currentStackIndex][4], array($nextObjectAssociativeIndex)), array_merge($stack[$currentStackIndex][5], array($nextObjectIndex))
				)
			);
		continue;};
			if( $stack[$currentStackIndex][0] === $value ){
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][4], $stack[$currentStackIndex][5]);
			};
		array_pop($stack);
	}
	return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the string address of the directory and subdirectories within which the file has to be located.  
Then the string value to locate inclusive of extension if present.  
 
RETURNS: null in case of wrong inputs, an empty array if no such value is found or an array of two items. The first is the item found at the end of each branch, and the second item is an array again, whose each entry is a number: that is the map of all the traversed indexes to get at the reported item.  
 
CAVEATS: case sensitive.
Printable version To exclude hyperlinks from the print, check the checkbox »
Rss

Cast your vote for this topic

To perform this operation it is not necessary to be Full Poster members.
«Negative Positive»
Click here to save your vote (Current average: 7.00, Voters: 3)
Are you the author of this topic and do you want to append quickly more text? click here
Other topics
This subscriber has a blog too: Read the blog: 57 Shoutbox: 103 reviews: 153
Visitors: 15,898
Overall visits to all the topics: 5,066,706
Daily average (Calculated from the website subscription day): 2,078.22
Optional sorting commands:
Normal order: click here
Order by amount of visits: click here
Order by category: Programming Php
Current order: normal
Other categories available for this author (Limited data report: 100):
Advice: Martial Arts and Self Defense(4), Books(236), Boxing(2), Critical Reviews and Essays(4), Dictionaries(1), Emergency Care(4), Epistles Letters and Advice(5), Fantasy Epics and Fables(1), History and Documents(5), Humor and Jokes(2), Methematics(2), Music and Lyrics(6), News Digests and Press Reviews(1), Novels Poetry and Stories(3), Philosophy Reviews(7), Poetry(1), Programming(4), Programming Ajax(2), Programming Javascript(82), Programming Php(52), Psychology(1), Quotes(1), Religion Esoterica and Spirituality(13), Scientifical Reviews(4), Self Improvement(1), Sport Activities and Apparels(1), Tarots(1)
Showing topics: 11, 20
Available total: 450
View only a list of the snippets by this author: click here.
Other topics available for this author: click on any title below to view the complete item:
Creative Writing: How To Write A Novel. Best Tips From The Bester Professionals Identification Number: 457 Visitors: 7,902 Newsreel Pseudo Intel: The Middle East Approaching The New 20s Identification Number: 456 Visitors: 7,777 Aforismatica Italiana Dell' Intelletto Al Cospetto Dello Spaventevole Identification Number: 455 Visitors: 7,840 Tradizione E Transizione: Insufficienze Logiche Di Scienza Filosofia E Teologia Identification Number: 454 Visitors: 8,142 Sia Nulla E Il Nulla Fu: Epistemologia Fallimentare Di Significato E Nichilismo Identification Number: 390 Visitors: 13,270 Php Date: Full Disaggregated Date Difference Accounting Timezones And Daylight Identification Number: 453 Visitors: 8,960 PHP Date: Disaggregated Date Difference As Interlapsed Days Hours Minutes Second Identification Number: 452 Visitors: 9,198 Maria Angelillo: I Mantra. Citazioni Preferite E Commento Identification Number: 451 Visitors: 7,437 Oscar Wilde: The Soul Of Man (Under Socialism): Part 2 Identification Number: 450 Visitors: 7,244 Oscar Wilde: The Soul Of Man (Under Socialism): Part 1 Identification Number: 449 Visitors: 6,624
External services
This page of this subscriber uses external services: Hide