背景:
用户上传图片并点击“保存”按钮。
我成功地将图像保存在服务器中。我给了文件夹 777 的权限....
问题:
我试图打开图片,但显示不正确:图片网址
在此处输入图片说明
html
<button class ="save" onclick="test()">Save image to server</button>
脚本:
function test(){
var canvas = document.getElementById("0");
var dataURL = canvas.toDataURL(); // THE BASE 64 DATA
var dataFileName = document.getElementById('fileup').value.replace(/.*(\/|\\)/, ''); // GET THE FILE NAME THAT USER CHOSE
var dataFileType = dataFileName.split('.').pop();
$.ajax({
type: "POST",
url: "tamaker.php",
data: {
imgBase64: dataURL,
imgFileName: dataFileName,
imgFileType: dataFileType
}
}).done(function(o, imgFileName) {
console.log(o);
var response = JSON.parse(o);
console.log(response);
$('body').prepend('<img src="' + dataFileName+ '" style="height: 200px; width: auto;">');
});
}
保存.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if( isset($_POST['imgBase64']) && isset($_POST['imgFileName']) && isset($_POST['imgFileType']) ){
$fname = filter_input(INPUT_POST, 'imgFileName'); // THE FILENAME THE USER CHOSE IS RECEIVED VIA POST
$img = filter_input(INPUT_POST, 'imgBase64'); // THE BASE64 ENCODING RECEIVED VIA POST
$imgtype = filter_input(INPUT_POST, 'imgFileType'); // THE FILE TYPE / EXTENSION IS RECEIVED VIA POST
// STRIP OFF THE BEGINNING OF THE BASE64 DATA, BUT DEPENDS ON THE IMAGE TYPE.
// I COULD HAVE SIMPLIFIED THIS BUT USED IF STATEMENTS.
if ( $imgtype === 'png'){
$img = str_replace('data:image/png;base64,', '', $img);
};
if ( $imgtype === 'jpg' || $imgtype === 'jpeg'){
$img = str_replace('data:image/jpeg;base64,', '', $img);
};
if ( $imgtype === 'gif'){
$img = str_replace('data:image/gif;base64,', '', $img);
};
智慧大石
烙印99