Location: PHPKode > projects > SvnDashboard > 1.1.3.140/phpLib/functions/file.funcs.php
<?php

//=============================================================================
//
// Description:
// 	Various functions for finding and manipulating flat files
//
// Functions:
//	upwardFileSearch( String $filename, [String|Array $searchPaths] )
//	recursiveRequireOnce( String $filename )
//	locateFile( String $fileName, [String|Array $searchPaths] )
//
//=============================================================================

function locateFile( $fileName, $searchPaths = FALSE )
{
	//===========================================================================
	// Function: locateFile
	// Author: Brian Mullen
	// Date: 2008-10-21
	// Description:	Search all paths (non-recursive) passed (CWD if no paths
	//				passed) for $fileName.
	// Params:
	//	$fileName {string} Required:			filename for which to search (may include relative path information)
	//	$searchPaths {string/array} Optional:	Paths within which to search for $filename
	//									If not supplied, the CWD is used
	//
	// Returns: {string}	absolute path to file
	//===========================================================================

	$result = FALSE;

	//make sure paths are in an array
	if( $searchPaths === FALSE ) // -- no paths were passed
	{
		$searchPaths = array( '.'. DIRECTORY_SEPARATOR );
	}
	elseif( !is_array( $searchPaths ) ) // -- one path was passed, not in an array
	{
		$searchPaths = array( $searchPaths );
	}

	//search all paths specified for file
	foreach( $searchPaths as $path )
	{
		$abspath = realpath( $path );
		$filePath = $abspath . DIRECTORY_SEPARATOR . $fileName;

		//dont continue searching if found
		if( file_exists( $filePath ) )
		{
			$result = $filePath;
			break;
		}
	}

	return $result;
}

function recursiveRequireOnce($filename)
{
	//===========================================================================
	// Function: recursiveRequireOnce
	// Author: Bob Jackman
	// Date: 2008-10-21
	// Description:	Attempt to require_once() $filename from the CWD. If not
	//				found, move up the file tree until either the root
	//				directory is reached or the file is found. If found,
	//				require_once()
	//
	// Params:
	//	$fileName {string} Required:	filename for which to search (may include
	//							relative path information)
	//
	// Returns: {bool}	TRUE = success
	//				FALSE = failure
	//===========================================================================

	try // -- attempt to include the file
	{
		require_once($filename);
	}
	catch (Exception $e) // -- not directly includable, move up the directory tree
	{
		// ====================  Recursively Search for File
		$located = upwardFileSearch($filename, '.'.DIRECTORY_SEPARATOR);

		if ($located === false)
		{
			throw new Exception("recursiveRequireOnce: ".$filename." not found.");
		}
		else
		{
			require_once($located);
		}

		return true;
	}
}

function upwardFileSearch($filename, $searchPaths = false)
{
	//===========================================================================
	// Function: fileSearch
	// Author: Bob Jackman
	// Date: 2008-10-21
	// Description:	Recursively move up the file structure looking for
	//				$filename (retain relative path within $filename if
	//				present)
	// Params:
	//	$fileName {string} Required:		filename for which to search (may include relative path information)
	//	$paths {string/array} Optional:	Paths within which to search for $filename
	//								If not supplied, the CWD is used
	//
	// Returns: {string}	absolute path to file
	//===========================================================================

	// ====================  Initialize Variables
	if ($searchPaths === false) // -- no paths were passed
	{
		$searchPaths = array('./');
	}
	elseif (!is_array($searchPaths)) // -- one path was passed, not in an array
	{
		$searchPaths = array($searchPaths);
	}

	$resultPath = false;
	foreach ($searchPaths as $basePath) // -- loop through all previously defined search paths
	{
		// ====================  Create Initial Search Path
		$currentPath = "";
		$oldPath = ".";

		// ====================  Move up the file tree until document root is reached or file is located
		$depth = 0;
		do
		{
			if (isset($pathParts))
			{
				$oldPath = $pathParts['dirname'];
			}

			// ====================  Determine Next Search Path
			$currentPath = realpath($basePath);
			$currentPath .= '/'.$filename;
			$pathParts = pathinfo($currentPath);
			$pathParts['dirname'] = str_replace('/./', '/', $pathParts['dirname']);
			$currentPath = realpath($pathParts['dirname']);

			if ($currentPath !== false && $pathParts['dirname'] != $oldPath) // -- path exists and is not the same as the last one
			{
				// ====================  Check for Existence of file at last search path
				if (file_exists($currentPath.'/'.$pathParts['basename'])) // -- file exists, add file to $paths array
				{
					$resultPath = $currentPath.'/'.$pathParts['basename'];
				}
			}

			$depth--;
			$basePath = '../'.$basePath;
		} while ($pathParts['dirname'] != $oldPath && $resultPath === false); // -- continue moving up the file tree

		if ($resultPath !== false)
		{
			break;
		}
	}

	// ====================  Return Result
	return $resultPath;
}

function trailingSlash( $path )
{
	if (substr( $path, -1 ) != '/') // -- ensure trailing /
	{
		$path .= '/';
	}

	return $path;
}

?>
Return current item: SvnDashboard