0

Hi I have a DataURL https://www.dropbox.com/s/xrt465wlnuzwogf/data.rtf (I have tested the dataURL and it renders an image) that I'm sending to a php file. var myImage = the dataURL This is the JS:

$.ajax({
    type:"POST",
    url: "php/save.php",
    data: "img="+myImage+"&random="+random,
    success: function(){
    }
});

This is the PHP taken from here and modified a bit:http://j-query.blogspot.in/2011/02/save-base64-encoded-canvas-image-to-png.html this is the php:

<?php

$random = $_REQUEST['random'];
$img = $_REQUEST['img'];

// requires php5
define('UPLOAD_DIR', '/');
//$img = $_POST['img'];
//echo($img);
$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.';
?>

The file is being created on my server 2742img.png but at 0B (no data). Does anybody know what might be stoping the file from saving?

Thanks.

user2238083
  • 583
  • 2
  • 9
  • 21
  • Your issue is that the image data is not being transmitted correctly. Please see the following question for your solution: http://stackoverflow.com/questions/19124019/sending-image-data-over-ajax-from-jquery – Mysteryos May 07 '14 at 05:56
  • Thanks, I've tried to add this to my JS: myImage = myImage.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''); But still no joy, can you expand Mysteryos? Thank you for your time. – user2238083 May 07 '14 at 06:33
  • Don't forget the `contentType: "application/x-www-form-urlencoded; charset=UTF-8",` part for the ajax call. As for your PHP, you might try this piece of code at http://stackoverflow.com/questions/11511511/how-to-save-a-png-image-server-side-from-a-base64-data-string – Mysteryos May 07 '14 at 06:57
  • Thanks Mysteryos, no joy! I'm going round in circles. Might have to admit defeat. Thanks for your help. – user2238083 May 07 '14 at 07:46

0 Answers0