使用 php 查看上传到数据库的最后一个图像



我是 php 新手,我正在尝试使用 php 查看上传到数据库的最后一张图像。 插入代码运行良好,但选择代码产生了问题。

这是我的代码

<?php
if (isset($_POST['UploadImage'])) {
$image = $_FILES['image']['tmp_name'];
$image_type = $_FILES['image']['type'];
if ($image == '') {
echo "<script> alert('please choose picture') </script>";
} else if ($image_type != "image/jpeg" && $image_type != "image/png" && $image_type != "image/gif") {
echo "<script> alert('image format is not correct ') </script>";
} else {
$query = "INSERT INTO  image (ImageLink) VALUES ('" . $image . "')";
if (mysqli_query($db1, $query)) {
echo '<script> alert("image uploaded successfully") </script>';
} else {
echo '<script> alert("please try again") </script>';
}
}
if (mysqli_affected_rows($db1) > 0) {
$query_image = "SELECT ImageLink FROM image ORDER BY Image_Id DESC LIMIT 1;";
$result = mysqli_query($db1, $query_image);
while ($row = mysqli_fetch_assoc($result)) {
echo "<img width='450' height='600' src='data:image/jpeg; base64,".base64_encode($row['ImageLink'])."' >";
echo "<br>";
}
}
}?>

跑步时...图片未显示在页面中。 请帮助这样做... 谢谢

解决方案是:
HTML 代码

<table
style="border-style: solid; border-color: gray; margin-top: 17px; width: 100%; height: 50%; width: 100%">
<tr>
<td>please upload a picture :</td>
<td><input type="file" name="image" id="image"></td>
</tr>
<tr></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Upload Image" name="UploadImage"
id="UploadImage"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
</table>

PHP代码

<?php
if(isset($_POST["UploadImage"])){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if (isset($_POST["UploadImage"])) {
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
echo "The file " . basename($_FILES["image"]["name"]) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
$query = "INSERT INTO  image (ImageLink) VALUES ('" . $target_file . "')";
if (mysqli_query($db1, $query)) {
echo '<script> alert("Image Has been Uploaded Successfully") </script>';
} else {
echo '<script> alert("Please Try Again") </script>';
}
if (mysqli_affected_rows($db1) > 0) {
$query_image = "SELECT ImageLink FROM image ORDER BY Image_Id DESC LIMIT 1;";
$result = mysqli_query($db1, $query_image);
while ($row = mysqli_fetch_assoc($result)) {
// var_dump( $row );
echo "<img width='450' height='600' src='data:image/jpeg; base64,".base64_encode(file_get_contents($row['ImageLink']))."'>";
echo "<br>";
}
}
}
}

最新更新