Ajax Post Has Empty Values On The Server
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, data
Solution 1:
This is what I do for jQuery Post. I hope it helps you
$.post("upload_post.php",
{
ship_id: "407",
base64_upload: "ABCSFSAFGDGFA....."
},
function(data, status){
alert("post result: " + status+ " - data:" + data);
location.reload();
});
You fetch the data in php exactly the same.
Solution 2:
Once again, I'm answering my own question.
The problem was I have define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
and then $file = UPLOAD_DIR . uniqid() . '.png';
and this UPLOAD_DIR
directory doesn't really exists. So I added a check if it exists and then created the directory before creating the file:
$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$file2 = UPLOAD_DIR;
if(!file_exists($file2)){
mkdir($file2, 0777, true);
$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.'';
}
Post a Comment for "Ajax Post Has Empty Values On The Server"