| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 | <?phpclass File{		//Gère l'upload d'un fichier des vérifications au stockage dans le dossier file	public static function upload($index, $path, $maxSize=1048576, $extensions=array('jpg','png','jpeg','gif','bmp'), $trimParentDir=true){		if($trimParentDir) $path = str_replace('..','',$path);		if(!isset($_FILES[$index])) throw new Exception("L'index fichier '".$index."' n'existe pas");		if($_FILES[$index]['error'] != 0) throw new Exception("Erreur lors de l'envois du fichier : N° ".$_FILES[$index]['error']);		$extension = getExt($_FILES[$index]['name']);		if($_FILES[$index]['size'] > $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);		}			if (ob_get_contents()) 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'){		if (ob_get_contents()) 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;	}}?>
 |