I'm trying to save an image file out of a base64 string received from the client side.
So I have this ajax post:
$.ajax({type: "POST", url: "upload_post.php", data: postData, dataType: "text", success: function(result){
alert("post result: " + result + " - data:" + postData);
location.reload();
}});
Here is an example of postData
(which I know contains data):
{"ship_id":"407","base64_upload":"ABCSFSAFGDGFA....."}
Now here is a my php code that handles this post:
$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$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. '.$file.'';
The problem is the $_POST variables are always empty. why is that? json related? location.reload() related? and how do I fix it?
EDIT
I have got these variables posted with actual data by doing JSON.parse(postData)
on the ajax data. Now my problem is I still can't save the image file. any help?