Thursday, 24 October 2013

php include all files from a directory

Include all files from a directory:-


Here is small code snippet to show that how we can include all files from a particular directory .

$all_files =  glob( "path to the directory/*") ;
foreach ( $all_files  as $filename)
{
include $filename;
}

If we try to explain it , then please understand glob() will return an array of all files on the path given as parameter .Rest everything is familiar with you all . foreach is the loop to iterate every element of any array . So by this we are including all files .

We can go in some tricky way also . example :-


Include all php files only :-

We can use regex syntax to achieve our goal . We will be able to include all php files only by following method

$all_files =  glob( "path to the directory/*.php") ;
foreach ( $all_files  as $filename)
{
include $filename;
}

Here only one parameter in the glob() function is changed and it will pic up all files which ends with .php extension .

Please ask question if you feel need of any clarification .