显示数据库中路径的图像 [英] Display image from path in database
本文介绍了显示数据库中路径的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示我上传的图像并将该图像的路径保存在MySQL数据库中.
I want to display an image I have uploaded and saved the path of the image in a MySQL database.
下面是我用来上传图像并将路径保存到数据库中表的所有代码:
Below is all the code I have used to upload the image and to save the path to my table in the database:
HTML表单:
<!DOCTYPE html>
<html>
<body>
<form action="processfangstrapport.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="billed1" type="file" accept="image/jpeg">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>
</body>
</html>
我的过程php:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "fiskerirapporter";
//Opretter forbindelse til databasen
$conn = new mysqli($servername, $username, $password, $dbname);
//Check forbindelse
if ($conn->connect_error) {
die("Forbindelse mislykkedes: " . $conn->connect_error);
}
function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
if (!empty($_FILES["billed1"]["name"])) {
$file_name=$_FILES["billed1"]["name"];
$temp_name=$_FILES["billed1"]["tmp_name"];
$imgtype=$_FILES["billed1"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "images/".$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
$query_upload="INSERT into images_tbl (images_path,submission_date) VALUES
('".$target_path."','".date("Y-m-d")."')";
mysqli_query($conn, $query_upload) or die("error in $query_upload == ----> ".mysql_error());
}else{
exit("Error While uploading image on the server");
}
echo "Din fangstrapport er nu oprettet.";
}
$conn->close();
?>
数据库表的结构:
CREATE TABLE images_tbl(
id INT NOT NULL AUTO_INCREMENT unique,
images_path VARCHAR(200) NOT NULL,
submission_date DATE
);
推荐答案
将表数据加载到 $ result
并根据您的要求对以下查询进行修改.
Load the table data to $result
and apply modifications to the below query according to your requirement.
foreach ($result as $value){
$path = 'http://address-upto-the-image-directory/'.$value[imagepath];
echo "<img src='".$path."' />";
}
imagepath
是您在表中存储路径的字段名称.
Here imagepath
is the field name where you stored the path in your table.
这篇关于显示数据库中路径的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文