1

i have a function which can write image on server folder with PHP. i follow instruction on this link Can't save a HTML5 Canvas as Image on a server , my problem is, i want to show success message when image save. on my code, it just show message only when write image failed. here is my function :

$loc = $_POST['lokasi'];
    define('UPLOAD_DIR', $loc);
 $filename =  $_POST['nama-file'];
 $img = $_POST['img'];
 $img = str_replace('data:image/png;base64,', '', $img);
 $img = str_replace(' ', '+', $img);
 $data = base64_decode($img);
 $file = UPLOAD_DIR ;
 $success = file_put_contents($file, $data);
 print $success ? $file : 'Unable to save the file.';

thanks for your response. :)

Community
  • 1
  • 1
IIM NUR DIANSYAH
  • 434
  • 1
  • 8
  • 18

1 Answers1

0
$loc = $_POST['lokasi'];
define('UPLOAD_DIR', $loc);
$filename =  $_POST['nama-file'];
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR ;    
if (file_put_contents($file, $data)) {
    echo "Image saved";
} else {
    echo "Unable to save the file.";
}

Note: With file-uploads its better to use move_uploaded_file

Ramon Bakker
  • 1,075
  • 11
  • 24
  • i try to modify it and combine with javascript like this : if (file_put_contents($file, $data)) { echo ''; } else { echo "Unable to save the file."; } but why still no alert? is it because i code it on yii component ??? – IIM NUR DIANSYAH Mar 01 '16 at 03:38
  • But do you get the "Unable to save the file" message? Because the script looks fine.. I tested and it worked normally – Ramon Bakker Mar 01 '16 at 15:59
  • no, i get feedback "image saved" message, but i must see it with firebug. when i dont use firebug, i never get message anything. is it because i used yii ? – IIM NUR DIANSYAH Mar 02 '16 at 04:31
  • i try it without framework, it works well, but why on yii controller doesn't show anything :|? – IIM NUR DIANSYAH Mar 02 '16 at 05:02
  • I personally dont use yii myself and have never used it before, so i cant tell you. But because you do get the message Image saved, it look like you did'nt saved it correctly or something? – Ramon Bakker Mar 02 '16 at 05:05
  • my image save with no problem, i think it because yii controller doesn't allow direct javascript. – IIM NUR DIANSYAH Mar 02 '16 at 05:39