Home > Back-end >  How to check specifications of a base64 image before upload for secure uploading
How to check specifications of a base64 image before upload for secure uploading

Time:02-14

I am posting a base64 image with ajax to PHP...But I wanna check specs before move to image to folder...

  1. how to check if image is base64 image or not...
  2. how to check if image is larger than 1Mbyte...

or anything else to secure upload... Here is my php upload code:

 $img_teknik_1   = $_POST["base64image_1"];
 $img_teknik_1   = str_replace('data:image/png;base64,', '', $img_teknik_1);
 $img_teknik_1   = str_replace(' ', ' ', $img_teknik_1);
 $data_teknik_1  = base64_decode($img_teknik_1);
 $file_teknik_1  = UPLOAD_DIR . $resim_kodu . '_teknik_1.png';
 $success_1      = file_put_contents($file_teknik_1, $data_teknik_1);}

CodePudding user response:

You can simply use:

strlen($_POST["base64image_1"]) <= 1048576

to check the size of the image (1 KiB = 1024 Bytes)

base64_decode($_POST["base64image_1"]) !== false;

to check the encoding.

  •  Tags:  
  • php
  • Related