Location: PHPKode > scripts > IO Tools > io-tools/io_tools.php
<?php
/*
 _____                    ______ _   _ ______
|  __ \                   | ___ \ | | || ___ \
| |  \/_ __ ___  ___ _ __ | |_/ / |_| || |_/ /
| | __| '__/ _ \/ _ \ '_ \|  __/|  _  ||  __/
| |_\ \ | |  __/  __/ | | | |   | | | || |
 \____/_|  \___|\___|_| |_\_|   \_| |_/\_|
----------------------------------------------
(C)BOBAK 2008                             V4.0
----------------------------------------------
web   : http://www.greenphp.com
email : hide@address.com
----------------------------------------------
*/

/**
 * Tool to manage files
 *
 * @author  Bobak
 * @package io
 * @subpackage io
 */
class io
{
/**
 * Clean a filename
 *
 * @param string $Fichier Filename
 * @return string
 */
public static function Clean($Fichier)
{
//Replaces \ by /
$Out=str_replace('\\','/',$Fichier);

//Replaces /. / By /
$Out=str_replace('/./','/',$Out);

//Corrects .. /
$OutA=explode('/',$Out);
$Nbi=count($OutA);
for ($i=0;$i<$Nbi;$i++)
   {
   if ($OutA[$i]=='..')
    {
    $OutA[$i]='';
    if ($i>0) {$OutA[$i-1]='';}
    }
   }
if ($OutA) {$Out=implode('/',$OutA);}

//Removes duplicate /
$Out=ereg_replace('/{1,}','/',$Out);

//Removes bad characters
$Out=eregi_replace('[^a-z0-9._:/-]','_',$Out);

//Protects all characters dangerous for the SHELL
//$Out=escapeshellcmd($Out);

//Removes empty
$Out=str_replace(' ',"_",$Out);

return $Out;
}

/**
 * Check if file exists and back its true path
 *
 * @param string $Fichier Filename
 * @return string
 */
public static function Exist($Fichier)
{
if (file_exists($Fichier) AND $Fichier)
  {return realpath($Fichier);}
else
  {return FALSE;}
}

/**
 * List of files or directories (such as shell)
 *
 * @param string $Nom : *.*
 * @param bool $RepOnly : FALSE=File , TRUE=Directory
 * @return array
 */
public static function Dir($Nom="*.*",$RepOnly=FALSE)
{
$Info=pathinfo($Nom);

//Dir filtration
$Rep=self::info($Nom,'Dir');
$Filtre='^'.$Info['basename'].'$';
$Filtre=str_replace('.','\\.',$Filtre);
$Filtre=str_replace('*','.*',$Filtre);
$Filtre=str_replace('?','.',$Filtre);
$Out=array();

if ($FP=@dir($Rep.'.'))
  {
  while ($Fichier=$FP->read())
       {
       if (ereg($Filtre,$Fichier))
         {
         if ($RepOnly)
           {
           //Dirs and no . or ..
           if (is_dir($Rep.$Fichier)and($Fichier!=".")and($Fichier!=".."))
             {$Out[]=$Fichier;}
           }
           else
           {
           //Files and no MAC Icon
           if (is_file($Rep.$Fichier)and($Fichier!="Icon?"))
             {$Out[]=$Fichier;}
           }
         }
       }
   @$FP->close();
   }

return $Out;
}

/**
 * Returns information about a file
 *
 * @param string $Fichier Filename
 * @param string $Commande cmd (Full|Dir|File|Ext|Base)
 * @return string
 */
public static function Info($Fichier,$Commande='Full')
{
//Clean all
$Fichier=self::clean($Fichier);

//Split
$Info=pathinfo($Fichier);

//Fixes empty directory
if ($Info['dirname']=='.')
  {$Info['dirname']='';}
  else
  {$Info['dirname'].='/';}

//CMD
$Commande=strtolower($Commande);
switch ($Commande)
      {
      case "full": $Out=$Fichier; break;
      case "dir" : $Out=$Info['dirname']; break;
      case "file": $Out=$Info['basename']; break;
      case "ext" : $Out=$Info['extension']; break;
      case "base": $Out=$Info['filename']; break;
      default    : $Out=FALSE;
      }

return $Out;
}

/**
 * Make a multi directory
 *
 * @param string $Nom Directoy name
 * @return bool
 */
public static function MkDir($Nom)
{
if (self::Exist($Nom))
  {$Out=TRUE;}
  else
  {$Out=mkdir($Nom,0755,TRUE);}

return $Out;
}

/**
 * Remove a directory with all are contained
 *
 * @param string $Nom Directory name
 * @return bool
 */
public static function RmDir($Nom)
{
if (!self::Exist($Nom))
  {$Out=FALSE;}
  else
  {
  //Removes sub directory
  $Reps=self::Dir($Nom.'/*',TRUE);
  $Nb=count($Reps);
  for ($i=0;$i<$Nb;$i++)
    {self::RmDir($Nom.'/'.$Reps[$i]);}

  //Removes file is in directory
  $Fichiers=self::Dir($Nom.'/*',FALSE);
  $Nb=count($Fichiers);
  for ($i=0;$i<$Nb;$i++)
    {unlink($Nom.'/'.$Fichiers[$i]);}

  //end of remove
  $Out=@rmdir($Nom);
  }

return $Out;
}

/**
 * Remove all files that match a search
 *
 * @param string $Nom Filename to match
 * @return bool
 */
public static function Del($Nom)
{
//Infos
$Dir=self::Info($Nom,'Dir');
$Fichiers=self::Dir($Nom,FALSE);

$Nb=count($Fichiers);
if ($Nb)
  {$Out=TRUE;}
  else
  {$Out=FALSE;}

//Remove
for ($i=0;$i<$Nb;$i++)
  {$Out=@unlink($Dir.'/'.$Fichiers[$i]);}

return $Out;
}

/**
 * Sync 2 directorys
 *
 * @param string $RepS input directory
 * @param string $RepC output directory
 * @return bool
 */
public static function Rsync($RepS,$RepC)
{
$Out=TRUE;

//Input exist ?
if (!self::Exist($RepS))
  {$Out=FALSE;}
  else
  {
  $Out&=mkdir($RepC);

  //Copy sub directorys
  $Reps=self::Dir($RepS.'/*',TRUE);
  $Nb=count($Reps);
  for ($i=0;$i<$Nb;$i++)
    {$Out&=self::Rsync($RepS.'/'.$Reps[$i],$RepC.'/'.$Reps[$i]);}

  //Copy files in the directory
  $Fichiers=self::Dir($RepS.'/*',FALSE);
  $Nb=count($Fichiers);
  for ($i=0;$i<$Nb;$i++)
    {$Out&=copy($RepS.'/'.$Fichiers[$i],$RepC.'/'.$Fichiers[$i]);}
  }

return $Out;
}

/**
 * Save a array in file
 *
 * @param array/string $Data Data to save
 * @param string $Fichier Filename
 * @param string $EOL End of line
 * @param bool $Append Append mode
 * @return bool
 */
public static function FileSave($Data,$Fichier,$EOL="\n",$Append=FALSE)
{
$Out=FALSE;
//Opening file mode
if ($Append)
  {$Mode='a';}
  else
  {$Mode='w';}

//Opening in write mode file
if ($FId=fopen($Fichier,$Mode))
  {
  //Merge of array if possible
  if (is_array($Data))
    {$Data=implode($EOL,$Data);}

  //Save
  fputs($FId,$Data);
  $Out=fclose($FId);
  }

return $Out;
}

/**
 * Drop a line in a file
 *
 * @param string $Line To drop line
 * @param string $Fichier filename
 * @param string $EOL End of line
 * @param bool $IsEreg Ereg mode
 * @return bool
 */
public static function FileDelLine($Line,$Fichier,$EOL="\n",$IsEreg=FALSE)
{
$Out=FALSE;

if ($Data=@file($Fichier))
  {
  $Nb=count($Data);
  $UPdate=FALSE;

  if ($IsEreg)
    {
    //Search with ereg
    for ($i=0;$i<$Nb;$i++)
       {
       if (eregi($Line,$Data[$i]))
         {
         unset($Data[$i]);
         $UPdate=TRUE;
         }
       }
    }
    else
    {
    //Searching for Equality accurate
    $Line.=$EOL;
    for ($i=0;$i<$Nb;$i++)
       {
       if ($Data[$i]==$Line)
         {
         unset($Data[$i]);
         $UPdate=TRUE;
         }
       }
    }

  //The file has changed, thus saving
  if ($UPdate)
    {
    self::FileSave($Data,$Fichier,'');
    $Out=TRUE;
    }
  }

return $Out;
}
}

?>
Return current item: IO Tools