我是PHP
and的初学者Ajax query
。我正在尝试上传profile image
使用croppie.js plugin
. 当我上传时image
,图像会像1580192100.png
. 但我的问题是不可能retrieve the image
根据userid
.
这是我尝试过的代码。profilepic.php
页
<?php
session_start();
require_once "../auth/dbconnection.php";
if (isset($_POST['image'])) {
$croped_image = $_POST['image'];
list($type, $croped_image) = explode(';', $croped_image);
list(, $croped_image) = explode(',', $croped_image);
$croped_image = base64_decode($croped_image);
$image_name = time().'.png';
// Valid file extensions
$allowTypes = array( 'bmp', 'jpg', 'png', 'jpeg' , 'JPG');
// if(in_array($fileType, $allowTypes)){
$stmt = $conn->prepare("UPDATE users SET image = ? WHERE user_id= ?");
$stmt->bind_param("si", $image_name, $_SESSION['user_id']);
$stmt->execute();
if($stmt->affected_rows === 0);
file_put_contents('blog/'.$image_name, $croped_image);
echo 'Cropped image uploaded successfully.';
}else{
echo "ERROR: Could not prepare query: $stmt. " . mysqli_error($conn);
}
$stmt->close();
// mysqli_stmt_close($stmt);
?>
php fetch
代码是;
<?php
require_once 'auth/dbconnection.php';
$sql="SELECT image FROM users WHERE user_id= '".$_SESSION['user_id']."'";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$out= '<img src="data:image/png;base64,'.base64_encode($row['image']).'" alt="">';
echo $out;
}
}
}
?>
我不知道我哪里错了。请帮我。