upload file base64 encoded

if(!empty($_POST)){

  if(!empty($_FILES)){
   //--on upload file (image) get details & encoding base64 :Starts--

    $file_name = $_FILES['file']['name'];

    $file_size= $_FILES['file']['size'];

    $file_tmp= $_FILES['file']['tmp_name'];

    $file_ext = strtolower( end(explode('.',$file_name)));

    $file_type = pathinfo($file_name, PATHINFO_EXTENSION);

    $file_data = file_get_contents($file_tmp);

    $data_base64 = 'data:image/' . $file_type . ';base64,' . base64_encode($file_data);


    //--on upload(image) get details & encoding base64: Ends -
    //----- Get Data & upload From Data Base Decoded :Starts ----

    #Get info from data:image Like: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA.

#Example:$data_image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA.';

    //$data_base64_encoded = Your encoded data: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA

$data_base64_encoded = $data_base64; 

$data_image = $data_base64_encoded;

$data_pos   = strpos($data_image, ';');

$data_type  = explode(':', substr($data_image, 0, $data_pos))[1];

$data_type_split = explode( '/', $data_type );

$data_extn = $data_type_split[1]; 

$rand = (rand(10,10000));

// $temp_data = str_replace('data:image/png;base64,', '', $data_base64);

$temp_data = str_replace('data:image/'.$data_extn.';base64,', '', $data_base64_encoded);

$temp_data = str_replace(' ', '+', $temp_data);

$data_base64_decoded = base64_decode($temp_data);

    $new_file_name  = time().$rand.'.'.$data_extn;

$new_file = 'image/'.$new_file_name;

$new_success = file_put_contents($new_file, $data_base64_decoded);

//----- Get Data & upload From Data Base Decoded : Ends ------
  }
}

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action=”” method=”post” enctype=”multipart/form-data”>
<input type=”file” name=”file”>
<input type=”submit” name=”upload” >
</form>
</body>
</html>
Please follow and like us:
0