<?php
session_start();

// ضبط اعدادات اللغة العربية للصفحة 
header("content-type:text/html;charset=utf-8"); 
		
 		function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

		
include('../SimpleImage.php');
$targetDir = "../../img/$_SESSION[upload_folder]/";
if(isset($_FILES["myfile"]))
{
	$ret = array();
	
//	This is for custom errors;	
/*	$custom_error= array();
	$custom_error['jquery-upload-file-error']="File already exists";
	echo json_encode($custom_error);
	die();
*/
	$error =$_FILES["myfile"]["error"];
	//You need to handle  both cases
	//If Any browser does not support serializing of multiple files using FormData() 
	$fileName = $_FILES['myfile']['name'];
	$extension = getExtension($fileName);
 	$extension = strtolower($extension);  
 $allowed = array('pdf', 'png', 'jpg','jpeg'); 
 if (!in_array($extension, $allowed)) { 
 echo 'error'; 
 exit(); 
 } 

	$uniqid=uniqid();
	$fileName=$uniqid.".".$extension;
	if(!is_array($_FILES["myfile"]["name"])) //single file
	{
 	 
	
	$targetFile = $targetDir.$fileName;
	
   if(move_uploaded_file($_FILES["myfile"]["tmp_name"],$targetFile)){
   	

   }
   $ret[]= $fileName;
  
   
   
	}
	else  //Multiple files, file[]
	{
	  $fileCount = count($_FILES["myfile"]["name"]);
	  for($i=0; $i < $fileCount; $i++)
	  {
	  	$targetFile = $targetDir.$uniqid.".".$extension;
		if(move_uploaded_file($_FILES["myfile"]["tmp_name"],$targetFile)){
	}
	  	$ret[]= $fileName;
	  
	  	
	
	

	  
	  
	  }
	
	}
    echo json_encode($ret);
 }
 ?>