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


how to show message success after write file on php?

我有一个函数,可以使用PHP在服务器文件夹上写入图像。 我按照此链接上的说明进行操作 无法在服务器上将HTML5画布另存为图像,我的问题是,我想在保存图像时显示成功消息。 在我的代码上,它仅在写入图像失败时才显示消息。 这是我的函数:

$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