<?php


session_start();   
$isvalid = false;
$newFileName = '';
$iscategory = false;
if(!isset($_SESSION['tfbs'])){
     
}else if($_SESSION['tfbs'] == null){
    

    
}else{
$db5=["stores","userlajeh","_*_100S"];
include("handler.php");$queries = new DatabaseFunctions($db5);
     $isvalid = true; 
}

?>
<?php
 
 if($isvalid){
     

  $message = ''; 
  if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK)
  {
    // get details of the uploaded file
    $fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
    $fileName = $_FILES['uploadedFile']['name'];
    $fileSize = $_FILES['uploadedFile']['size'];
    $fileType = $_FILES['uploadedFile']['type'];
    $fileNameCmps =  explode(".", $fileName);
    $fileExtension = strtolower(end($fileNameCmps));
 
    // sanitize file-name
    $newFileNameraw =$fileNameCmps[0];
    $newFileNameraw =explode("asdw_400_SABA_", $newFileNameraw);
    $newFileNameraw =end($newFileNameraw);
    $newFileNamecoderaw =explode("pic", $newFileNameraw);
    if(sizeof($newFileNamecoderaw) == 1){
          $iscategory = true;
        
        $newFileNamecoderaw =$newFileNameraw;
        $newFileName = time().$newFileNamecoderaw.'.'. $fileExtension;
        
     }else{
        
        $newFileNamecoderaw = end($newFileNamecoderaw);
       $newFileName = time().'pic'.$newFileNamecoderaw.'.'. $fileExtension;
    }
    
        
        $newFileNametodupload = "asdw_400_SABA_".$newFileName;
      
    // check if file has one of the following extensions
    $allowedfileExtensions = array('jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc');
 
    if (in_array($fileExtension, $allowedfileExtensions))
    {
      // directory in which the uploaded file will be moved
      $uploadFileDir = "../imgs/";
      $dest_path = $uploadFileDir . $newFileNametodupload;
 
      if(move_uploaded_file($fileTmpPath, $dest_path)) 
      {
          
        if($iscategory){
             $queries -> updateyad("EXEC updatecatpic @pic = N'".$newFileName."', @cod = ".$newFileNamecoderaw);
        }else{
             $queries -> updateyad("EXEC updatepic @pic = N'".$newFileName."', @cod = ".$newFileNamecoderaw);
            
        }
          
       
        $message ='File is successfully uploaded.';
      }
      else
      {
        $message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
      }
    }
    else
    {
      $message = 'Upload failed. Allowed file types: ' . implode(',', $allowedfileExtensions);
    }
  }
  else
  {
    $message = 'There is some error in the file upload. Please check the following error.<br>';
    $message .= 'Error:' . $_FILES['uploadedFile']['error'];
  }
echo $newFileName; 
     
     
      }else{
     
 }

?>