Location: PHPKode > scripts > NabiCI > include/folders.php
<?php
/**
 * Things which are missing in PHP to handle folders. Obligatory Layer.
 * @author Axel Benz
 * @package file_system_access
 *
 */
require_once("string_utilities.php");
require_once("easy_file.php");

class easy_folder{
	/**
	 * Delete a folter
	 *
	 * @param string $file
	 * Path to the folder.
	 * @param string $necessary_path_part
	 * A string which MUST be in the path, otherwise this branch will not be deleted.
	 *
	 */
	public static function delete_folder($file,$necessary_path_part=""){
		$file = realpath($file);
		if ($necessary_path_part=="" or ! (strpos($file,$necessary_path_part)===false)){
			if (is_dir($file)){
				$handle = opendir($file);
				while($filename = readdir($handle)){
					if ($filename != "." && $filename != ".."){
						self::delete_folder($file."/".$filename);
					}
				}
				closedir($handle);
				error_reporting(E_ERROR);
				chmod($file,0777);
				error_reporting(E_ALL);
				@rmdir($file);
			}
			else{
				if (file_exists($file)){
					error_reporting(E_ERROR);
					chmod($file,0777);
					error_reporting(E_ALL);
					unlink($file);
				}
			}
		}
	}
	/**
	 * Set a complete folder structure with all files writabe for everybody.
	 *
	 * @param string $file
	 * The folder to start with.
	 * @param string $necessary_path_part
	 * A string which MUST be in the path. Otherwise this branch is not processed.
	 *
	 */
	public static function set_writeable($file,$necessary_path_part=""){
		#on unix, it is not possible to set the mode of an existing, not freshly created file.
		#even if it is already at 0777
		error_reporting(E_ERROR);
		if ($necessary_path_part=="" or !(strpos(realpath($file),$necessary_path_part)===false)){
			chmod($file,0777);
			error_reporting(E_ALL);
			if (is_dir($file)){
				$handle = opendir($file);
				while($filename = readdir($handle)){
					if ($filename != "." && $filename != ".."){
						self::set_writeable($file."/".$filename);
					}
				}
				closedir($handle);
			}
		}
	}


	/**
	 * For all files in source_dir, look whether the there are files in target_dir with the same
	 * names as the files in source_dir. Look recursively in subdirectories. All patterns use
	 * only this slashes: /, no backslashes.
	 *
	 * @param string $source_dir
	 * The source directory. Path without slash at the end.
	 *
	 * @param string $target_dir
	 * The the second directory. Path without slash at the end
	 *
	 * @param ArrayOfString exclude_extensions.
	 * Array of file extensions which are excluded from search.
	 *
	 * @param boolean verbose
	 * If true, the function prints out the name of the files which it has not found.
	 *
	 * @param string exclude_pattern
	 * If this pattern is found in target, searching target will not go deeper from here.
	 *
	 *
	 * @return boolean
	 * True, if all the files of $source_dir are in $target_dir
	 */
	public static function files_of_source_are_in_target($source_dir,$target_dir,$exclude_extensions=array(), $verbose=false,$exclude_pattern_ar=array()){
		$source_dir = str_replace("\\","/",$source_dir);
		$target_dir = str_replace("\\","/",$target_dir);
		
		$path_parts = pathinfo($target_dir);
		if (isset($path_parts["extension"]) and in_array($path_parts["extension"],$exclude_extensions))
		{
			return true;
		}
		foreach ($exclude_pattern_ar as $exclude_pattern){
			$search_target_dir = str_replace("\\","/",$target_dir);
			if ($exclude_pattern != "" and ! (strpos($search_target_dir,$exclude_pattern)===false)){
				return true;
			}
		}

		if (is_dir($source_dir)){
			$handle = opendir($source_dir);
			while($filename = readdir($handle)){
				if ($filename != "." && $filename != ".."){

					$abso_filename = str_replace("\\","/",realpath($source_dir."/".$filename));
					$extension = str_dif_from_right($abso_filename,$source_dir);
					$searched_file = $target_dir.$extension;

					if (! self::files_of_source_are_in_target($abso_filename,$searched_file,$exclude_extensions,$verbose,$exclude_pattern_ar))
					{
						return false;
					}
				}
			}
			closedir($handle);
			return true;
		}else{
			clearstatcache();
			$res = easy_file::exists($target_dir);
			if (!$res and $verbose){
				echo "<BR> File not found:".$target_dir;
				echo "<BR> searched because of:.".realpath($source_dir)."<BR>";
			}
			return $res;
		}
	}

	/**
	 * Copy a folder and all its contents from one directory to another
	 *
	 * @param string $source the path to the file or directory you want to copy to another directory
	 * @param string $target the path to the directory where the files shall be stored
	 */
	public static function copy( $source, $target )
	{
		//is the source a directory?
		if ( is_dir( $source ) )
		{
			//create target directory
			if (file_exists($target)){easy_folder::delete_folder($target);}

			self::create($target);

			//Handler for source directory (read, reset, close)
			$d = dir( $source );
			//read in every single file from the source directory
			while ( FALSE !== ( $entry = $d->read() ) )
			{
				//if the actual file is a "link", i.e. "." or ".."
				if ( $entry == '.' || $entry == '..' )
				{
					//then read in the next file of the source directory
					continue;
				}

				$Entry = $source . '/' . $entry;
				//if the file is a directory
				if ( is_dir( $Entry ) )
				{
					//then copy the whole folder
					self::copy( $Entry, $target . '/' . $entry );
					//and go on with the next file
					continue;
				}
				//if the actual file is neither a "link" nor a directory
				//we can simply copy the file
				copy( $Entry, $target . '/' . $entry );
			}
			//close source directory
			$d->close();
			//if the source is a simple file
		}else
		{
			//then just copy the file from the source directory to the target directory
			if (! file_exists($target)){mkdir($target,0777);}
			copy( $source, $target );
		}
	}

	/**
	 * Create a folder recursively.
	 *
	 * @param String $target
	 * The absolute path to the folder to create.
	 */
	public static function create($target){
		$target = str_replace("\\","/",$target);
		$folder_chain = explode("/",$target);
		$root = "";
		foreach ($folder_chain as $folder){
			$root = $root.$folder;
			clearstatcache();
			#switch warnings off, because even "is_dir" can create warnings and will not work then
			error_reporting(E_ERROR);
			if (! is_dir($root) and $root != ""){
				mkdir( $root,0777);
				chmod($root,0777);
			}
			error_reporting(E_ALL);
			$root = $root . "/";
		}
	}
}
?>
Return current item: NabiCI