Jump to content

Include All


cugopob.vlad

Recommended Posts

Not sure if its been posted before, i tried searching but couldnt really find anything similar to this. Anyway, i put together a little php script that includes all files within the same directory. The purpose of this? Well, if you have a library folder or something similar where you keep all your php scripts, instead of including every single page, you could just include one that would automatically include the rest of them. Now, if you just wanted to include a few select scripts, this obviously wont do you any help. It will also include all files in sub folders. Oh, i forgot to mention that this page would have to be at the root of you php library. If you see any improvements that could be made, please let me know. Thanks! Here's the code:

 

<?php

$thisFileFull = __FILE__;
$thisFile = basename($thisFileFull);
$thisDir = dirname($thisFileFull).'/';

$folders = array();
$files = array();

/*
*	Scanner:
*	Scan the directory and store its contents in appropriate arrays
*	If a file is found, store in $files array 
*	If a directory is found, store in the $folders array
*/
function scanner($folder)
{
global $folders,$files,$thisFile;

if($_contents = opendir($folder))	//open the directory
{
	while(false !== ($file = readdir($_contents)))	//loop through the contents of the directory
	{
		if($file == "." || $file == ".."){}else
		{
			if(is_dir($folder.$file))	//only processes folders 
			{
				if(!in_array($folder.$file,$folders))array_push($folders,$folder.$file);	//add the folder to folders array if it doesnt already exists
			}
			elseif(is_file($folder.$file) && $file != $thisFile)	//only processes files and make sure not to include this file, otherwise you'd be in an infinite loop
			{
				if(!in_array($folder.$file,$files))array_push($files,$folder.$file);	//add the file to files array if it doesnt already exists
			}
		}
	}
}
}
/*
*	Cleaner:
*	"Cleans" the folders array 
*/
function clean()
{
global $folders;

foreach($folders as $key => $newFolder)	//loop through the folders array
{
	scanner($newFolder.'/');	//send all the results to be rescanned, 
	unset($folders[$key]);	//after scanning remove the folder from the array
}		
}
/*
*	Scan through the root and return ALL of the results
*/
function scan()
{
global $folders,$thisDir,$files;

if(!is_dir($thisDir))	//check to see if the directory is valid before continuing
{
	return false;	
}
elseif(isset($thisDir))	//check to see if the root directory is set
{
	scanner($thisDir);	//initial scan of the root directory
	while(!empty($folders))	//loop through to check if there are any folders in the root directory
	{
		clean();	//rescan all folders in the root directory
	}
	if(empty($folders))	//checks to see that there are no more folders
	{
		return $files;	//returns the results(the entire url leading to the file) ex. /var/www/index.php instead of index.php
	}
}
}

foreach(scan() as $key => $value)
{
include_once($value);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...