File indexing completed on 2024-05-12 17:26:16

0001 <?php
0002 
0003 /*
0004  *   TRT GFX 3.0.1 (beta build) BackToSlash
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: http://trt-gfx.googlecode.com
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the MIT License.
0011  *   http://opensource.org/licenses/mit-license.php
0012  */ 
0013 
0014 
0015 /*
0016  *  Static class used as a helper for filesystem operations.
0017  */
0018  
0019 class EFileSystem{
0020   
0021   /*
0022    * Function that return file extension in string format if exists,
0023    * return false in the opposite case
0024    */
0025   public static function get_file_extension($nome){
0026     $trova_punto = explode(".", $nome);
0027     $estensione = $trova_punto[count($trova_punto) - 1];
0028     $estensione = strtolower($estensione);
0029     if (isset($trova_punto[1]) == FALSE){
0030       return false;
0031     }
0032     return $estensione;
0033   }
0034   
0035   /*
0036    * Function that return file name in string format if exists,
0037    * return false in the opposite case.
0038    * 
0039    * It actually erases every extension found.
0040    * filename.inc.conf --> filename
0041    */
0042   public static function get_file_name($nome){
0043     $chunks = explode(".", $nome);
0044     if (isset($chunks[0]) == FALSE){
0045       return false;
0046     } else {
0047       return $chunks[0];
0048     }
0049   }
0050   
0051   /*
0052    * Function that renames a file, mantaining the correct extension
0053    */
0054   public static function rename_file($from,$to){
0055     if(rename($from,$to)){
0056       return true;
0057     } else {
0058       return false;
0059     }
0060   }
0061   
0062   public static function rename_file_ext($from,$to){
0063       $ext = EFileSystem::get_file_extension($from);
0064       if(rename($from,$to.".".$ext)){
0065           return true;
0066       } else {
0067           return false;
0068       }
0069   }
0070   
0071   /*
0072    * Get an uploaded file and moves it to $path with $newname
0073    * mantaining file extension.
0074    */
0075   public static function move_uploaded_file_in_ext($path,$newname=false){
0076     $nfile = $_FILES['localfile']['name'];
0077     if(move_uploaded_file($_FILES['localfile']['tmp_name'], getcwd()."/".$path.$nfile)){
0078       if($newname){
0079         EFileSystem::rename_file_ext($path.$nfile,$path.$newname);
0080       }
0081       return true;
0082     } else {
0083       return false;
0084     }
0085   }
0086   
0087   /*
0088    * Get an uploaded file and moves it to $path with $newname
0089    * without mantainig file extension (you have to specify it).
0090    */
0091   public static function move_uploaded_file_in($path,$newname=false){
0092     $nfile = $_FILES['localfile']['name'];
0093     if(move_uploaded_file($_FILES['localfile']['tmp_name'], getcwd()."/".$path.$nfile)){
0094       if($newname){
0095         EFileSystem::rename_file($path.$nfile,$path.$newname);
0096       }
0097       return true;
0098     } else {
0099       return false;
0100     }
0101   }
0102   
0103   /*
0104    * Get an uploaded file and moves it to $path with $newname
0105    */
0106   public static function get_uploaded_file_name(){
0107     if(isset($_FILES['localfile']['name'])){
0108       return $_FILES['localfile']['name'];
0109     }
0110   }
0111   
0112   public static function rmdir($path){
0113     if(is_dir($path)){
0114       EFileSystem::clean_all_files_in($path);
0115       rmdir($path);
0116     }
0117   }
0118   
0119   /*
0120    * erase all files from a folder, except those given in array
0121    */
0122   public static function clean_all_files_in($path="",$safe=""){
0123     if(empty($safe)){
0124       $safe = array();
0125     }
0126     
0127     
0128     if(!empty($path)){
0129       $prevpath = getcwd();
0130       if(is_dir($path)){
0131         chdir($path);
0132         
0133         foreach(glob("*") as $filename){
0134           if(is_file($filename)){
0135             if(in_array($filename,$safe)){
0136               continue;
0137             } else {
0138               unlink($filename);
0139               continue;
0140             }
0141           }
0142         }
0143         
0144         chdir($prevpath);
0145       }
0146     }
0147   }
0148   
0149 }
0150 
0151 ?>