如何在 php 上写入文件后显示消息成功?

how to show message success after write file on php?

我有一个可以用 PHP 在服务器文件夹上写入图像的功能。我按照此 link Can't save a HTML5 Canvas as Image on a server 的说明进行操作,我的问题是,我想在图像保存时显示成功消息。在我的代码中,它仅在写入图像失败时才显示消息。这是我的功能:

$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.';

感谢您的回复。 :)

$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.";
}

注意:对于文件上传,最好使用 move_uploaded_file