<?php

function bytesToSize1024($bytes, $precision = 2)
 {
    $unit = array('B','KB','MB');
    return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), $precision).' '.$unit[$i];
}

		$sFileName = $_FILES['image_file']['name'];
		$sFileType = $_FILES['image_file']['type'];
		$sFileSize = bytesToSize1024($_FILES['image_file']['size'], 1);

		// OK valide
		//$target_path = "uploads/";
		//$target_path = $target_path . basename( $_FILES['image_file']['name']); 
		//$tmp_name = $_FILES["image_file"]["tmp_name"];
		//move_uploaded_file($tmp_name, $target_path);
		
		// traitement pour renomer le fichier existant
		if(file_exists("uploads/" .$_FILES['image_file']['name']))
		{
				$fichier = $_FILES['image_file']['name'];
				$infos = explode('.', $fichier);
				$nouveau_nom = $infos[0] .'_'.date('d-m-Y-H-i-s') ."." .$infos[1];
				$fichier_path = "uploads/" .$nouveau_nom;
				$tmp = $_FILES['image_file']['tmp_name'];
				move_uploaded_file($tmp, $fichier_path);
				echo "Renamed";
		}
		else
		{
				$fichier = $_FILES['image_file']['name'];
				$nouveau_nom = $fichier;
				$fichier_path = "uploads/" .$nouveau_nom;
				$tmp = $_FILES['image_file']['tmp_name'];
				move_uploaded_file($tmp, $fichier_path);
		}
		
echo <<<EOF
<p>Your file: {$sFileName} has been successfully received.</p>
<p>Type: {$sFileType}</p>
<p>Size: {$sFileSize}</p>
EOF;

?>

