$maxSize) throw new Exception("Taille du fichier trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)'); if(!in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier non permise, autorisé :".implode(', ',$extensions)); $filePath = str_replace(array('/','\\','{{ext}}'),array(SLASH,SLASH,$extension),$path); $fileName = basename($filePath); $folderPath = dirname($filePath); $folderAbsolutePath = self::dir().$folderPath; $fileAbsolutePath = $folderAbsolutePath.SLASH.$fileName; if(!file_exists($folderAbsolutePath)) mkdir($folderAbsolutePath,0755,true); if(!move_uploaded_file($_FILES[$index]['tmp_name'], $fileAbsolutePath)) throw new Exception("Impossible de déplacer le fichier envoyé"); return array( 'absolute' => $fileAbsolutePath, 'relative' => $filePath, 'name' => $fileName ); } //récupere le type mime du fichier public static function mime($path){ $infos = new finfo(); return $infos->file($path, FILEINFO_MIME_TYPE); } public static function move($file,$path,$trimParentDir = true){ if($trimParentDir){ $file = str_replace('..','',$file); $path = str_replace('..','',$path); } $extension = getExt($path); $filePath = str_replace(array('/','\\','{{ext}}'),array(SLASH,SLASH,$extension),$path); $fileName = basename($filePath); $folderPath = dirname($filePath); $folderAbsolutePath = self::dir().$folderPath; $fileAbsolutePath = $folderAbsolutePath.SLASH.$fileName; if(!file_exists($folderAbsolutePath)) mkdir($folderAbsolutePath,0755,true); if(!rename($file, $fileAbsolutePath)) throw new Exception("Impossible de déplacer le fichier envoyé"); //New version //Testée, pour le moment pas de pb survenus return array( 'absolute' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileAbsolutePath)) : $fileAbsolutePath), 'relative' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($filePath)) : $filePath), 'name' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileName)) : $fileName) ); //Old version // return array( // 'absolute' => $fileAbsolutePath, // 'relative' => $filePath, // 'name' => $fileName // ); } public static function delete($namespace,$file,$trimParentDir = true){ if($trimParentDir) $file = str_replace('..','',$file); $file = $namespace.SLASH.substr($file, strlen($namespace)+1); $file = File::dir().$file; $file = str_replace(array('\\','/'),array(SLASH,SLASH),$file); if(!file_exists($file)) throw new Exception("Le fichier '".$file."' n'existe pas"); unlink($file); } public static function downloadFile($path,$name=null,$mime = null,$forceDownload = null,$trimParentDir = true){ if($trimParentDir) $path = str_replace('..','',$path); if(!file_exists($path)) throw new Exception("Fichier inexistant :".$path); $stream = file_get_contents($path); $mime = !isset($mime) ? self::mime($path) : $mime; if(!isset($name)){ $name = (get_OS() === 'WIN') ? utf8_encode(basename($path)) : basename($path); } ob_end_clean(); header("Content-Type: ".$mime); header("Content-Length: " . strlen($stream)); header('Expires: Sun, 01 Jan 2014 00:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('Pragma: no-cache'); header("Content-Disposition: ".($forceDownload?"attachment":"inline")."; filename=\"".utf8_decode($name)."\""); echo $stream; } public static function downloadStream($stream, $name='Sans titre', $mime='application/octet-stream'){ ob_end_clean(); header("Content-Type: ".$mime); header("Content-Length: " . strlen($stream)); header('Expires: Sun, 01 Jan 2014 00:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('Pragma: no-cache'); header("Content-Disposition: attachment; filename=\"".$name."\""); echo $stream; } public static function dir(){ return __ROOT__.FILE_PATH; } public static function temp(){ $path = self::dir().SLASH.'tmp'.SLASH; if(!file_exists($path)) mkdir($path,0755,true); return $path; } public static function clear_temp(){ $time = time(); foreach(glob(self::temp().'*.*') as $file){ if($time - filemtime($file) > 3600) unlink($file); } } public static function copy($source,$destination,$trimParentDir = true){ if($trimParentDir) $source = str_replace('..','',$source); if($trimParentDir) $destination = str_replace('..','',$destination); if(is_file($source)) return copy($source,$destination); $dir = opendir($source); if(!file_exists($destination)) mkdir($destination,0755,true); while(false !== ( $file = readdir($dir)) ) { if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir($source . SLASH . $file) ) { self::copy($source.SLASH.$file,$destination.SLASH.$file); } else { copy($source.SLASH.$file,$destination.SLASH.$file); } } } closedir($dir); } public static function convert_encoding($path){ return get_OS() === 'WIN' ? utf8_encode($path) : $path; } public static function convert_decoding($path){ return get_OS() === 'WIN' ? utf8_decode($path) : $path; } } ?>