如何从提交的数据中正确获取PHP中的base64值?
正在传递的数据是
data:image/png;base64,LongBase64ValueHereOfAnImage
现在,我只能通过
来获取它$data = $_POST['image'];
list($type, $data) = explode(';', $data);
list($data) = explode(',', $data);
$data = base64_decode($data);
是否存在获取base64值的正确方法?
答案 0 :(得分:2)
这可以使用正则表达式完成一行
$data = $_POST['image'];
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
答案 1 :(得分:0)
尝试:上传图片代码。
if (count($_FILES) > 0) {
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$conn = mysqli_connect('localhost', 'username', 'password', 'databasename');
$imgData = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$imageProperties = getimageSize($_FILES['image']['tmp_name']);
$sql = "INSERT INTO upload(image) VALUES('$imgData')";
$current_id = mysqli_query($conn, $sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error($conn));
if (isset($current_id)) {
echo 'done';
}
}
}
然后使用base64获取数据
$sql = "SELECT * FROM upload WHERE id = 113";
$sth = $conn->query($sql);
$result = mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,' . base64_encode($result['image']) . '"/>';