<?php 
include('config.php');
include('utils.php');

$ds          = DIRECTORY_SEPARATOR; 
 
$storeFolder = $_POST['path'];
$storeFolderThumb = $_POST['path_thumb'];  
 
if (!empty($_FILES) && $upload_files) {
     
    $tempFile = $_FILES['file']['tmp_name'];   
      
    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; 
    $targetPathThumb = dirname( __FILE__ ) . $ds. $storeFolderThumb . $ds; 
     
    $targetFile =  $targetPath. $_FILES['file']['name']; 
/*	$targetFile_n=substr($_FILES['file']['name'],0,strrpos($_FILES['file']['name'],'.'));
	$targetFile_e=substr(strrchr($_FILES['file']['name'],'.'),1);
	$inval=array('.',',','"',"'",';','>','<'); $targetFile_n=str_replace($inval,'_',$targetFile_n);
	if (!preg_match("/^[A-Za-z0-9_-]{2,50}$/",$targetFile_n)) {$targetFile=uniqid("img_").".".$targetFile_e;
	$targetFile=$targetPath.$targetFile;}
	else $targetFile=$targetPath.$targetFile_n.'.'.$targetFile_e;*/
	
    $targetFileThumb =  $targetPathThumb. $_FILES['file']['name']; 
 $imageinfo = getimagesize($_FILES['file']['tmp_name']);
    if(in_array(substr(strrchr($_FILES['file']['name'],'.'),1),$ext_img) && substr($imageinfo['mime'],0,5)=="image" ) $is_img=true;
	else $is_img=false;

	if($is_img){
		create_img_gd($tempFile, $targetFileThumb, 156, 78);

		$imginfo =getimagesize($tempFile);
		$srcWidth = $imginfo[0];
   		$srcHeight = $imginfo[1];
		
		if($image_resizing){
			
			if($image_width==0){
				if($image_height==0){
					$image_width=$srcWidth;
					$image_height =$srcHeight;
				}else{
					$image_width=$image_height*$srcWidth/$srcHeight;
			}
			}elseif($image_height==0){
				$image_height =$image_width*$srcHeight/$srcWidth;
			}
			$srcWidth=$image_width;
			$srcHeight=$image_height;
			create_img_gd($tempFile, $targetFile, $image_width, $image_height);
		}else
			move_uploaded_file($tempFile,$targetFile);

		//max resizing limit control
		$resize=false;
		if($image_max_width!=0 && $srcWidth >$image_max_width){
			$resize=true;
			echo "resizione";
			$srcHeight=$image_max_width*$srcHeight/$srcWidth;
			$srcWidth=$image_max_width;
		}
		
		if($image_max_height!=0 && $srcHeight >$image_max_height){
			$resize=true;
			$srcWidth =$image_max_height*$srcWidth/$srcHeight;
			$srcHeight =$image_max_height;
		}
		if($resize)
			create_img_gd($targetFile, $targetFile, $srcWidth, $srcHeight);	
		
	}else{
		move_uploaded_file($tempFile,$targetFile);
	}
	
     
}
?>      