这是一个php代码。它不工作。它存储在图像的db名称中,而不是图像。 我没有找到这个问题,但我找不到解决方案。我尝试了file_get_contents()但不起作用。我不知道服务器或代码是否有问题。
<?php
$msg="";
if(isset($_POST['submit'])){
session_start();
$target_dir="uploads/";
$target_file=$target_dir . basename($_FILES["fileToUpload"]["name"]);
include 'dbh.php';
$image=$_FILES['fileToUpload']['name'];
$image_tmp=$_FILES['fileToUpload']['tmp_name'];
$id=$_SESSION['id'];
$sql="UPDATE user SET image='$image' WHERE id='$id'";
mysqli_query($conn,$sql) or Die("ERROR:" .mysqli_error($conn));
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
$msg="Image uploaded successfully";
}
else{
$msg="There was a problem uploading image";
}
}
?>
<form action="user_photo.php" method="post" enctype="multipart/form-data" target="iframe">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" name="submit" value="Ngarko Foto">
</form>
答案 0 :(得分:1)
file_get_contents
应该有效。但是图像数据是二进制的,因此您无法将其替换为SQL。你应该使用准备好的声明。
$image = file_get_contents($_FILES['fileToUpload']['tmp_name']);
$sql = "UPDATE user SET image = ? WHERE id = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "bi", $image, $_SESSION['id']);
mysqli_stmt_execute($stmt) or die(mysqli_error("ERROR:" .$conn));