Your IP : 216.73.216.201


Current Path : /home/balossw/www/temp/common/includes/
Upload File :
Current File : /home/balossw/www/temp/common/includes/files.inc.php

<?php
/*---------------------------------------------------------------------------
		File name ............ comon/includes/files.inc.php
		Author ............... Antoine Lemoine
		Creation date ........ 02.09.2009
		Modification date .... 22.10.2009
---------------------------------------------------------------------------*/


function addEndingSlash($path)
{
	$length = strlen($path);
	if ($length>0 && $path{$length-1}!='/') return $path.'/';
	else return $path;
}

function buildPath($path1, $path2)
{
	return addEndingSlash($path1).$path2;
}

function mkdirs($path, $chmod)
{
	mkdir_r($path,$chmod);
}

function mkdir_r($path, $chmod)
{
	$path = cleanPath($path);
	$folder_hierarchy = explode('/',$path);
	$fullpath = '';
	foreach ($folder_hierarchy as $folder)
	{
		$fullpath .= $folder.'/';
		if (!file_exists($fullpath)) @mkdir($fullpath,$chmod);
	}
}

// Fonction qui liste les fichiers d'un dossier
function listDirectory($path, &$files_array, $list_folders=false, $list_files=true, $recursive=false, $subfolder='')
{
	if (!is_array($files_array)) $files_array = array();
	$path = addEndingSlash($path);
	$subfolder = addEndingSlash($subfolder);

	if (is_dir($path.$subfolder) && $dir = opendir($path.$subfolder)) {
		while ($file = readdir($dir)) {
			if ($file!='.' && $file!='..' && $file!='.htaccess') {
				$fullname = $subfolder.$file;
				if (is_dir($path.$fullname)) {
					if ($list_folders) $files_array[] = $fullname;
					if ($recursive) listDirectory($path, $files_array, $list_folders, $list_files, true, $fullname);
				}
				else if ($list_files) $files_array[] = $fullname;
			}
		}
		closedir($dir);
		sort($files_array);
	}
}

function controlPath($root, $filename)
{
	$root = addEndingSlash($root);
	$filename = $root.$filename;
	if (!is_dir($filename)) $filename = dirname($filename);
	$filename_realpath = realpath($filename);
	$root_realpath = realpath($root);
	return substr($filename_realpath,0,strlen($root_realpath))==$root_realpath;
}

function cleanPath($path)
{
	$result = array();
	$protocol = '';

	if (preg_match('#^([a-z0-9]+://)(.*)$#i',$path,$preg_result)) {
		$protocol = $preg_result[1];
		$path = $preg_result[2];
	}

	$folder_hierarchy = explode('/',$path);
	if (!isset($folder_hierarchy[0])) $result[] = '';
	$i = 0;
	foreach ($folder_hierarchy as $folder)
	{
		if ($folder=='..') {
			if (end($result)=='..' || !array_pop($result)) $result[] = '..';
		}
		elseif ($folder!='' && ($folder!='.' || $i==0)) $result[] = $folder;
		$i++;
	}
	if ($result[0]=='.') array_shift($result);
	return $protocol.implode('/',$result);
}

function formatFileSize($size)
{
	if ($size>1048576) return number_format($size/1048576,2,'.','').' Mo';
	else if ($size>1024) return ceil($size/1024).' Ko';
	else return ceil($size).' o'/*.($size>1 ? 's' : '')*/;
}

function getMimeType($extension)
{
	$mime_type_array = array(
		'.gz' => 'application/x-gzip',
		'.tgz' => 'application/x-gzip',
		'.zip' => 'application/zip',
		'.pdf' => 'application/pdf',
		'.png' => 'image/png',
		'.gif' => 'image/gif',
		'.jpg' => 'image/jpeg',
		'.jpeg' => 'image/jpeg',
		'.txt' => 'text/plain',
		'.htm' => 'text/html',
		'.html' => 'text/html',
		'.mpg' => 'video/mpeg',
		'.avi' => 'video/x-msvideo',
	);
	return isset($mime_type_array[$extension]) ? $mime_type_array[$extension] : 'application/octet-stream';
}

function forceDownload($filename)
{
	if (!file_exists($filename)) return false;
	if (headers_sent())
	{
		trigger_error('forceDownload($filename) - Headers have already been sent',E_USER_ERROR);
		return false;
	}

	$extension = strrchr($filename,'.');
	$mime_type = getMimeType($extension);
	$infos = pathinfo($filename);

	header('Content-disposition: attachment; filename="'.$infos['basename'].'"');
	header('Content-Type: application/force-download');
	header('Content-Transfer-Encoding: '.$mime_type."\n"); // Surtout ne pas enlever le \n
	header('Content-Length: '.filesize($filename));
	header('Pragma: no-cache');
	header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
	header('Expires: 0');
	readfile($filename);
	flush();
	return true;
}

// Ecrire un fichier de maniere un peu sure
function writeFile($file, $content)
{
	if (!file_exists($file)) mkdirs(dirname($file),0755);

	$fp = @fopen($file, 'wb');
	$s = @fputs($fp,$content,$a=strlen($content));
	$ok = ($s==$a);
	@fclose($fp);
	if (!$ok) @unlink($file);
	return $ok;
}

?>