<?php

	if(($_SERVER['REQUEST_METHOD'] == 'POST') && (isset($_POST['modify']))){



		// remove spaces from image link

		$images = preg_replace('/\s\s*/', null,$_FILES['p_images']['name']);

		$images_tmpies = $_FILES['p_images']['tmp_name'];

		// set rendom string to avoid image link dublication
		$rand = randomSTR();

		//stor all errors in this array
		$errors = [];


		// errors 
		if(empty($images)) $errors[] = 'Veuillez selectionez au moin une image !';

		for($i = 0; $i < count($images_tmpies) ; $i++ ) {

				$imageFileType = pathinfo($images[$i],PATHINFO_EXTENSION);

				if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
						&& $imageFileType != "gif" ) {
						    $errors[] =  "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

						}else{

							$imgs[] = $rand.$images[$i];
							$img_tmpies[] = $images_tmpies[$i];
						}
			}




				// if there is no errors upload image

			if(empty($errors)){

				foreach ($partner as $parnerarray) { // bring old images 

					$del_img = explode('/', $parnerarray['p_photo']);

				}
				
				foreach ($del_img as  $del) { // delet old images

					if(!empty($del) AND (file_exists(UPIMG.$del))){
						unlink(UPIMG.$del);
					}

				}

				$imagestoup = implode('/', $imgs).'/';

				$sql   = "UPDATE partners SET p_photo = '$imagestoup' WHERE p_id='$id'";
				$stmt  = $con->prepare($sql);
				$donne = $stmt->execute();
				if($donne){ // updated success fully

					for($i = 0; $i < count($images_tmpies); $i++){

						move_uploaded_file($images_tmpies[$i],UPIMG.$rand.$images[$i]);
					}

					alert('success','Success',' Les images ont été modifiées avec succès');
					leave(1,'partenaires.php');

				}else{ alert('danger','Oops ','L\'image ne peut pas être modifiée'); } // not updated 





			} // empty errors -------------------------------------


				foreach ($errors as  $erra) {
					alert('danger','Erreur', $erra);
				}

	}



?>