I have the following code :
<?php
header('Content-Type: image/png');
$data = "iVBORw0KGgoAAAANSUhEUgAAAuAAAAI8CAYAAACwIh5dAAAgAElkZJgRCXxOAuIPzzTlg......";
define('UPLOAD_DIR', '/home/Desktop/image.png');
$img = $data;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
Here $data
contain the encoded string of image in base-64. I want to save the image into the file. I did the previous code also go through lot of tutorial in google but it's not working.
Can anyone help me where I am going wrong.
Thanks in advance.
First specify the correct path like :
define('UPLOAD_DIR', '/home/Desktop/');
After check that the path you provided has read/write permission set or not.
I believe this line:
define('UPLOAD_DIR', '/home/Desktop/image.png');
Should be:
define('UPLOAD_DIR', '/home/Desktop/');
define('UPLOAD_DIR', '/home/xpointers/Desktop/');
Remember! Your apache webserver will run with its own user rights (e. g. www-data). Take care, that your apache user or the group apache runs in have access to your desired directory. To test you can chmod 777 to this directory and see what happens.
Writing inside a user directory is not always a good idea. If possible use a neutral directory where the needed users can access.