Problems with file_put_contents PHP

0

I have a problem with that function, sometimes I save the image, sometimes I do not and I do not know why, I have done everything and nothing. Could someone help me?

Here the code.

<?php
  $croped_image = $_POST['dataImage'];
  list($type, $croped_image) = explode(';', $croped_image);
  list(, $croped_image) = explode(',', $croped_image);
  $croped_image = base64_decode($croped_image);
  $image_name = time().'.png';
  //Guardamos la imagen
  file_put_contents('../eventos/'.$image_name, $croped_image);
?>
    
asked by Venté 30.11.2017 в 02:00
source

1 answer

0

The format of a dataURI is

data:[<mime type>][;charset=<charset>][;base64],<encoded data>

Therefore there may be 0, 1 or 2 characters ; .

I would do a split for a regular expression assuming that the last element of the array is the data itself:

<?php
$dataUri ='data:[<mime type>][;charset=<charset>][;base64],<encoded data>';

$exploded_uri = preg_split("/[,;]+/",$dataUri);

$encoded_data = array_pop($exploded_uri);

print_r($encoded_data);
    
answered by 30.11.2017 / 13:16
source