drus39136 2017-06-18 05:36
浏览 40

使用PHP Error 500在服务器上保存.toDataURL()

I have this function from a GitHub repo that outputs a drawn signature on canvas to a DataURL(). When I inspect it, it comes back as a string of encoded data(a .png).

The user clicks the save button and this happens:

saveButton.addEventListener("click", function (event) {
    if (signaturePad.isEmpty()) {
        alert("Please provide signature first.");
    } else {
        saveSignature(signaturePad.toDataURL());
    }
});

function saveSignature(dataURL) {
$.ajax({
  type: "POST",
  datatype: "json",
  url: "script.php",
  data: { 
     imgBase64: dataURL
  }
}).done(function(o) {
  console.log('saved'); 
});
signaturePad.clear();
}

Then it triggers a PHP script in the same folder, called script.php.

<?php
    // requires php5
    define('UPLOAD_DIR', 'images');
    $img = $_POST['imgBase64'];
    $file = UPLOAD_DIR . uniqid() . '.png';
    $success = file_put_contents($file, $data);
    print $success ? $file : 'Unable to save the file.';
?>

I can't find out why it's causing a 500 server error.

The console isn't printing 'unable to save file', nor 'saved.'

  • 写回答

1条回答 默认 最新

  • dongyunque2511 2017-06-18 05:46
    关注

    You have error in script.php you are receving data in $img while write using $data so need to replace variable

    <?php
        // requires php5
        define('UPLOAD_DIR', 'images');
        $img = $_POST['imgBase64'];
        $file = UPLOAD_DIR . uniqid() . '.png';
        $success = file_put_contents($file, $img); //<---- change here
        print $success ? $file : 'Unable to save the file.';
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn