2017-06-18 114 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