How to Include() All PHP Files from a Directory

How to include() all PHP files from a directory?

foreach (glob("classes/*.php") as $filename)
{
include $filename;
}

How to include all PHP files in directory?

I always do that like this where you would put your include normally but then in a foreach:

foreach(glob('dir/*.php') as $file) {
include_once $file;
}

that is maybe not the best way, it is always a good idea to create a list maybe an array of filepaths and then put that in the foreach like:

$includes = array(
'path/to/file.php',
'path/to/another/file.php'
);

foreach($includes as $file) {
include_once $file;
}

then whenever you add a file you can add one to that list and it will be included

Include php files when they are in different folders

You can get to the root from within each site using $_SERVER['DOCUMENT_ROOT']. For testing ONLY you can echo out the path to make sure it's working, if you do it the right way. You NEVER want to show the local server paths for things like includes and requires.

Site 1

echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/';

Includes under site one would be at:

echo $_SERVER['DOCUMENT_ROOT'].'/includes/'; // should be '/main_web_folder/includes/';

Site 2

echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/blog/';

The actual code to access includes from site1 inside of site2 you would say:

include($_SERVER['DOCUMENT_ROOT'].'/../includes/file_from_site_1.php');

It will only use the relative path of the file executing the query if you try to access it by excluding the document root and the root slash:

 //(not as fool-proof or non-platform specific)
include('../includes/file_from_site_1.php');

Included paths have no place in code on the front end (live) of the site anywhere, and should be secured and used in production environments only.

Additionally for URLs on the site itself you can make them relative to the domain. Browsers will automatically fill in the rest because they know which page they are looking at. So instead of:

<a href='http://www.__domain__name__here__.com/contact/'>Contact</a>

You should use:

<a href='/contact/'>Contact</a>

For good SEO you'll want to make sure that the URLs for the blog do not exist in the other domain, otherwise it may be marked as a duplicate site. With that being said you might also want to add a line to your robots.txt file for ONLY site1:

User-agent: *
Disallow: /blog/

Other possibilities:

Look up your IP address and include this snippet of code:

function is_dev(){
//use the external IP from Google.
//If you're hosting locally it's 127.0.01 unless you've changed it.
$ip_address='xxx.xxx.xxx.xxx';

if ($_SERVER['REMOTE_ADDR']==$ip_address){
return true;
} else {
return false;
}
}

if(is_dev()){
echo $_SERVER['DOCUMENT_ROOT'];
}

Remember if your ISP changes your IP, as in you have a DCHP Dynamic IP, you'll need to change the IP in that file to see the results. I would put that file in an include, then require it on pages for debugging.

If you're okay with modern methods like using the browser console log you could do this instead and view it in the browser's debugging interface:

if(is_dev()){
echo "<script>".PHP_EOL;
echo "console.log('".$_SERVER['DOCUMENT_ROOT']."');".PHP_EOL;
echo "</script>".PHP_EOL;
}

Include all PHP files from a directory

function includeFilesFromDir ( $dir_path, $file_extension, $recursive = false ){
if(!is_dir( $dir_path )){
return false;
}
if(empty( $file_extension )){
return false;
}
$all_dir_files = scandir( $dir_path );
foreach($all_dir_files as $file){
if( $file == "." || $file == ".." ){
continue;
}
if( is_dir( $dir_path . "/" . $file ) && $recursive ){
includeFilesFromDir( $dir_path . "/" . $file, $file_extension, $recursive);
} elseif( $file_extension == pathinfo( $file, PATHINFO_EXTENSION ) ){
include( $dir_path . "/" . $file );
}

}

return true;
}
includeFilesFromDir("C:/some_dir", "php", true);

Include files from parent or other directory

include() and its relatives take filesystem paths, not web paths relative to the document root. To get the parent directory, use ../

include('../somefilein_parent.php');
include('../../somefile_2levels_up.php');

If you begin with a /, an absolute system file path will be used:

// Full absolute path...
include('/home/username/sites/project/include/config.php');

PHP include files from another directory that also include files

The best way, to prevent changes in CWD that might break relative paths is to include files using absolute paths.

An easy way to accomplish this is by using the __DIR__ constant.

Example:

File structure:

serverRoot (/)
|-usr
|-local
|-www
|-index.php
|-bootstrap.php
|-includes
|-a.php
|-overall
|-b.php
|-c.php

let's say that:

  • index.php includes bootstrap.php and a.php
  • a.php includes b.php
  • bootstrap.php includes c.php

index.php

$basedir = realpath(__DIR__);
include($basedir . '/bootstrap.php');
include($basedir . '/includes/a.php');

a.php

global $basedir;
include($basedir . '/includes/overall/b.php');

bootstrap.php

global $basedir;
include($basedir . '/includes/overall/c.php');

Include all files in a folder - PHP

PHP's include shouldn't be used for other file types, like .json. To extract data from those files you'll want to read them using something like file_get_contents. For example:

$data = json_decode(file_get_contents('someFile3.json'));

To recursively include the PHP files in other directories you can try recursively searching through all directories:

function require_all($dir, $max_scan_depth, $depth=0) {
if ($depth > $max_scan_depth) {
return;
}

// require all php files
$scan = glob("$dir/*");
foreach ($scan as $path) {
if (preg_match('/\.php$/', $path)) {
require_once $path;
}
elseif (is_dir($path)) {
require_all($path, $max_scan_depth, $depth+1);
}
}
}

$max_depth = 255;
require_all('folder3', $max_depth);

This code is a modified version of the code found here: https://gist.github.com/pwenzel/3438784



Related Topics



Leave a reply



Submit