2017-06-18 80 views
0

我具有由GitHub库上帆布拉伸签名输出到DataURL此函数()。当我检查它时,它会以一串编码数据(一个.png)的形式返回。.toDataURL()使用PHP错误500

用户点击保存按钮,出现这种情况:

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(); 
} 

然后在同一文件夹触发一个PHP脚本,叫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.'; 
?> 

我找不出它为什么会导致500服务器错误。

控制台不打印“无法保存文件”,也不是“救”。

+0

检查你的httpd或Apache日志或检查'images'路径,看看它是否真的工作作为预期。 – codekaizer

+0

我不知道如何访问该信息。我只是在Coda上使用Web控制台。这个网站的上传到domain.com服务器...是否有可能的PHP不会在一定条件下运行 - 它需要放置在具有权限的特殊文件夹?如果我尝试从url栏访问php,是否应该返回“Internal Server Error”? – yeeeeee

回答

1

你有错误script.php您在$img receving数据使用$data而写那么需要更换可变

<?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.'; 
?> 
+0

仍然给出相同的错误... – yeeeeee