<?php
// Check if the form was submitted



// get the absolute path to $file


if($_SERVER["REQUEST_METHOD"] == "POST"){
   if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
       $filename = $_FILES["photo"]["name"];
        $filetype = $_FILES["photo"]["type"];
        $filesize = $_FILES["photo"]["size"];
    
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
       
       $maxsize = 50 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
          if(file_exists($filename)){
                echo $filename . " is already exists.";
            } else{
                move_uploaded_file($_FILES["photo"]["tmp_name"],  $filename);
               
			   $file = $filename ;

			   $path = pathinfo(realpath($file), PATHINFO_DIRNAME);

				$zip = new ZipArchive;
				$res = $zip->open($file);
				if ($res === TRUE) {
				  $zip->extractTo($path);
				  $zip->close();
				  echo "WOOT! $file extracted to $path";
				} else {
				  echo "Doh! I couldn't open $file";
				}
				
				
				
				$Identifire=$_POST['Identifire'];

$inp = file_get_contents('data.json');
$tempArray = json_decode($inp);


$existingPacks=json_decode($inp);

$newObject = new stdClass;

$newObject->identifier = $Identifire;
$newObject->name = $_POST['name'];
$newObject->publisher = "🌺 गणेश चतुर्थी स्टिकर्स 🌺";
$newObject->tray_image_file = "01.webp";
$newObject->publisher_email = "";
$newObject->publisher_website = "";
$newObject->privacy_policy_website = "";
$newObject->license_agreement_website = "";
$newObject->stickers =  array();

if ($handle = opendir('./'.$Identifire )) {
    while (false !== ($entry = readdir($handle))) {


        if ($entry != "." && $entry != "..") {

			$sticker=new stdClass();
			

			$iparr = explode (".",$entry);
			$sticker->image_file=$iparr[0];
			$sticker->emojis=array();
			
			array_push($newObject->stickers,$sticker);

            //echo $iparr[0];
        }
    }

    closedir($handle);
}

$packs=json_decode($inp)->sticker_packs;




array_push($packs,$newObject);

//$inp->sticker_packs.push($newObject);
$existingPacks->sticker_packs=$packs;
echo json_encode($existingPacks);

file_put_contents('data.json', json_encode($existingPacks));
				
				
            } 
       // } else {
        //    echo "Error: There was a problem uploading your file. Please try again."; 
       // }
    } else{
        echo "Error 11--: " . $_FILES["photo"]["error"];
    }
}
?>