Source for file FilesystemToolkit.class.php
Documentation is available at FilesystemToolkit.class.php
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
* Including various functions to manipulate arrays.
require_once( CLASSPATH. "core/Arrays.class.php" );
* Constant FILESANDFOLDER describes that files and folder kan match this value.
* For example if you want to read both (file and folders) from a filesystem.
* Constant FILES is defined to declare the type files in a filesystem.
* Constant FOLDER is defined to declare the type folder in a filesystem.
* @author Daniel Plücken ( daniel@debakel.net )
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (c) 2003 Daniel Plücken ( daniel@debakel.net )
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* Removes the last slash in a path if it is on the last position.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $string The string comprises the path and the filename
* and which should be slited.
// return preg_replace( "!(.+?)/ *$!", "$1", $string );
if( ( $strpos = strrpos( $string, '/' ) ) === strlen( $string ) - 1 )
return substr( $string, 0, $strpos );
* Return human readable sizes.
* @author Aidan Lister <aidan@php.net>
* @link http://aidanlister.com/repos/v/function.size_readable.php
* @param int $unit The maximum unit
* @param int $retstring The return string format
* @param int $si Whether to use SI prefixes
function size_readable( $size, $unit = null, $retstring = null, $si = true )
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
$sizes = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
if ($unit === null || $unit === false) {
if ($retstring === null) {
$retstring = '%01.2f %s';
while ($unit != $i && $size >= 1024 && $i < $ii) {
return sprintf($retstring, $size, $sizes[$i]);
* Returns the suffix of a file. To be returned, the suffix has to have three
* or four digits and contains only letters (ASCII) and numbers.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $path The string containing the path to the file to
{ return preg_replace( "!.+\.([a-z0-9]{3,4})$!i", "$1", $path ); }
* Separates the path and the filename of each other and returns the splited
* values in an array. The first entry [0] of the array will be the path and
* the second entry [1] will be the filename.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $string The string comprises the path and the filename
* and which should be slited.
$arr[0] = substr( $string, 0, $pos );
$arr[1] = substr( $string, $pos + 1 );
* Extracs the direct path from one to another folder.
* The from-folder is absolut the target-folder is relative.
* For example you read out links from a htmlsource and you want to follow
* this links. You know the url of the file's htmlsource and where the links
* you read out relativly refer to. Then you just should give the url $from
* and the relative path $to to this function and it will return you the
* direct url to the links in the htmlsource.
* @author Daniel Plücken <daniel@debakel.net>
while ( $toArr[0] == "." )
for ( $i = 0; $i < count( $toArr ); $i++ )
if ( $toArr[$i] == ".." )
while ( $toArr[0] == ".." )
$frompath = implode( "/", $fromArr );
$outputstr = ( !empty( $frompath )
* Loads the content of a given folder. Stores file- and foldernmaes in a two
* dimensional array like as follows:
* $out[FILENUMBER]['name']
* $out[FILENUMBER]['is_dir']
* $out[FILENUMBER]['size'] (if flag $filesize is set)
* The output array will be sorted by the filename.
* @todo Modifying the sort that files and folders will be grouped in the
* @author Daniel Plücken <daniel@debakel.net>
* @param string $regExpMatchFiles
* @param string $regExpMatchFolder
* @param boolean $filesize
# folder and files to separate arrays.
while ( ( $file = readdir( $folder ) ) !== false )
empty( $regExpMatchFolder )
$out[$i]["name"] = $file;
$out[$i]["is_dir"] = true;
empty( $regExpMatchFiles )
$out[$i]["name"] = $file;
$out[$i]["is_dir"] = false;
$out[$i]["size"] = filesize( $dir. "/". $file );
* Loads the content of a given file.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $path Where the file can be found.
* @param string $file The file's name whose content should be loaded.
if ( empty( $param[1] ) )
* Copies a directory recursively.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $source The directory to copy.
* @param string $target The target directory for copying.
function copyDir( $source, $target )
while ( ( $file = readdir( $folder ) ) !== false )
if ( is_dir( $source. "/". $file ) )
if ( !is_dir( $target. "/". $file ) )
mkdir( $target. "/". $file );
// temporarly file content
* Deletes a directory recursively.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $dir The directory to delete.
while ( ( $file = readdir( $folder ) ) !== false )
if( is_dir( $dir. "/". $file ) )
* Deletes a directory recursively. This method is an alias for the method
* @author Daniel Plücken <daniel@debakel.net>
* @param string $dir The directory to delete.
* Rewrites a directory recursively to handle it like it is written by the
* @author Daniel Plücken <daniel@debakel.net>
* @param string $dir The directory to rewrite.
while ( ( $file = readdir( $folder ) ) !== false )
if ( is_dir( $dir. "/". $file ) )
// temporarly file content
* Checks whether an url can access.
* @author Fabrizio (staff at bibivu dot com)
* @author Daniel Plücken <daniel@debakel.net> (only write down here)
* @see http://www.php.net/manual/en/function.file-exists.php
* @param string $url The url to proof of existence.
if ( !isset ( $a_url["port"] ) )
$page = isset ( $a_url["path"] )
$page .= isset ( $a_url["query"] )
"HEAD ". $page. " HTTP/1.0\r\n"
. "Host: ". $a_url["host"]. "\r\n\r\n"
$head = fread( $fid, 4096 );
return preg_match( '#^HTTP/.*\s+[200|302]+\s#i', $head );
} // END of class FilesystemToolkit
|