Source for file class.afiles.php
Documentation is available at class.afiles.php
* @package linea21.externals
* @subpackage Absynthe-files
* @author Sylvain 'Absynthe' Rabot <sylvain@abstraction.fr> , modified by Linea21 <info@linea21.com>
* @link http://absynthe.is.free.fr/aFiles/
* @license http://opensource.org/licenses/gpl-3.0.html
* Default path to an entity
* Constructor which allows to chose the path of the entity used for following actions
* Methods which allows to chose the path of the entity used for following actions
* Return the path of the entity used by default
* Return the type of an entity
function type($path = null)
* Return the list of all entity included in the path
* @param boolean $withroot
function ls($path = null, $withroot = true)
while (false !== ($file = readdir($handle)))
if ($file != '..' && $file != '.' && $file != '')
$infos[] = $path. '/'. $file;
$infos = array_map(array($this, 'format_path'), $infos);
* Return the list of directories included in the path
* @param boolean $withroot
function lsd($path = null, $withroot = true)
while (false !== ($file = readdir($handle)))
if ($file != '..' && $file != '.' && $file != '')
$infos[] = $path. '/'. $file;
$infos = array_map(array($this, 'format_path'), $infos);
* Return infos of all entity included in the path
* @param boolean $withroot
function ll($path = null, $withroot = true)
while (false !== ($file = readdir($handle)))
if ($file != '..' && $file != '.' && $file != '')
$infos[$temp] = $this->infos($temp);
$infos[$temp] = $this->infos($path. '/'. $file);
* Return infos of all directories included in the path
* @param boolean $withroot
function lld($path = null, $withroot = true)
while (false !== ($file = readdir($handle)))
if ($file != '..' && $file != '.' && $file != '')
$infos[$temp] = $this->infos($temp);
$infos[$temp] = $this->infos($path. '/'. $file);
* Create a file with or without content
function mkfile($content = '', $path = null)
if ($handle = fopen($path, 'w+'))
* Read the content of a file
function read_file($path = null, $byline = false, $length = 1024)
if ($handle = fopen($path, 'r'))
$lines[] = fgets($handle, $length);
* Create a directory with/without chmod
function mkdir($path = null, $chmod = null)
* Move a file or a directory
function mv($path, $where)
$tree = $this->tree($path);
$this->cp($path, $where);
$this->cp($path, $where);
* Remove files or/and directories
function rm($path = null)
$tree = $this->tree($file);
* Copy files or/and directories
function cp($path, $where)
copy($path, $where. '/'. $file_name);
copy($file, $where. '/'. $file_name);
$files = $this->tree($file);
$this->mkdir($where. '/'. $file);
$this->mkdir($where. '/'. $f);
* Return the mod of a file/directory
* Credits goes to Ambriel_Angel (www dot ambriels dot net)
$val += (($perms & 0x0100) ? 0x0100 : 0x0000); // Read
$val += (($perms & 0x0080) ? 0x0080 : 0x0000); // Write
$val += (($perms & 0x0040) ? 0x0040 : 0x0000); // Execute
$val += (($perms & 0x0020) ? 0x0020 : 0x0000); // Read
$val += (($perms & 0x0010) ? 0x0010 : 0x0000); // Write
$val += (($perms & 0x0008) ? 0x0008 : 0x0000); // Execute
$val += (($perms & 0x0004) ? 0x0004 : 0x0000); // Read
$val += (($perms & 0x0002) ? 0x0002 : 0x0000); // Write
$val += (($perms & 0x0001) ? 0x0001 : 0x0000); // Execute
$val += (($perms & 0x40000) ? 0x40000 : 0x0000); // temporary file (01000000)
$val += (($perms & 0x80000) ? 0x80000 : 0x0000); // compressed file (02000000)
$val += (($perms & 0x100000) ? 0x100000 : 0x0000); // sparse file (04000000)
$val += (($perms & 0x0800) ? 0x0800 : 0x0000); // Hidden file (setuid bit) (04000)
$val += (($perms & 0x0400) ? 0x0400 : 0x0000); // System file (setgid bit) (02000)
$val += (($perms & 0x0200) ? 0x0200 : 0x0000); // Archive bit (sticky bit) (01000)
* Return infos concerning the entity
* @param boolean $withroot
* @param boolean $content
function infos($path = null, $withroot = true, $content = true, $byline = false, $length = 1024)
$infos['chmod'] = $this->mod($path);
$infos['files_count'] = 0;
$infos['size'] = $this->filesize($path);
$infos['files'] = array();
$infos['directories'] = array();
while (false !== ($file = readdir($handle)))
if ($file != '..' && $file != '.' && $file != '')
$infos['directories'][] = $path. '/'. $file;
$infos['directories'][] = $file;
$infos['files'][] = $path. '/'. $file;
$infos['files'][] = $file;
$infos['files'] = array_map(array($this, 'format_path'), $infos['files']);
$infos['directories'] = array_map(array($this, 'format_path'), $infos['directories']);
if ($handle = fopen($path, 'r'))
$infos['chmod'] = $this->mod($path);
$infos['lines_count'] = 0;
$infos['size'] = $this->filesize($path);
$infos['content'][] = fgets($handle, $length);
* Return tree of a directory
* @param boolean $expand2files
function tree($path = null, $expand2files = true)
$directories = $this->lsd($path);
for ($x = 0; $x < count($directories); $x++ )
if (!is_dir($directories[$x]))
if ($handle = opendir($directories[$x]))
while (false !== ($file = readdir($handle)))
if (is_dir($directories[$x]. "/". $file))
if ($file != '..' && $file != '.' && $file != '')
$directories[] = $directories[$x]. "/". $file;
$directories = array_map(array($this, 'format_path'), $directories);
foreach ($directories as $dir)
$expanded_directories[] = $dir;
while (false !== ($file = readdir($handle)))
$expanded_directories[] = $dir. '/'. $file;
$expanded_directories[] = false;
$expanded_directories = array_map(array($this, 'format_path'), $expanded_directories);
return $expand2files === true ? $expanded_directories : $directories;
* Return the size of an entity
$tree = $this->tree($path);
* Serialize and creates a file with the serial
function parse_ini($path = null, $whithsection = true)
function mkini($content, $path = null)
foreach ($content as $key => $ini)
$out .= "\n[". $key. "]\n\n";
foreach ($ini as $var => $value)
$out .= $var. " \t\t= ". $this->quote_ini($value). "\n";
$out .= $key. " \t\t= ". $this->quote_ini($ini). "\n";
return $this->mkfile($out, $path);
------------------------------------------------- */
* Set a variable to $default parameter if it's null
* @param anything $default
* Replace '//' by '/' in paths
* Quote ini var if needed
/*function quote_ini($var)
return is_string($var) === true ? '"'.str_replace('"', '\"', $var).'"' : $var;
* Apply the callback function on files with extension "filter"
* this method is recursive
function lsr($directory, $filter= false, $directory_tree)
// if the path has a slash at the end we remove it here
if(substr($directory,- 1) == '/')
$directory = substr($directory,0,- 1);
// if the path is not valid or is not a directory ...
// ... we return false and exit the function
// ... else if the path is readable
$directory_list = opendir($directory);
// and scan through the items inside
while (false !== ($file = readdir($directory_list)))
// if the filepointer is not the current directory
// or the parent directory
if($file != '.' && $file != '..')
// we build the new path to scan
$path = $directory. '/'. $file;
// if the path is readable
// we split the new path by directories
$subdirectories = explode('/',$path);
// if the new path is a directory
// we scan the new path by calling this function
$directory_tree= $this->lsr($path, $filter, $directory_tree);
// if the new path is a file
// get the file extension by taking everything after the last dot
// if there is no filter set or the filter is set and matches
if($filter === false || $filter == $extension)
if(isset ($directory_tree))
// if the path is not readable ...
|