gpt4 book ai didi

php - 在数据库中插入图像文件,然后使用php显示它

转载 作者:行者123 更新时间:2023-11-29 18:00:48 25 4
gpt4 key购买 nike

我正在尝试使用 php 在数据库中插入图像,然后使用另一个 php 文件显示它..但我在显示它时遇到问题..通过这段代码,我尝试将图像插入到我的数据库中,该图像存储在photography.php中:

<?php
$servername = "********";
$username = "*******";
$password = "*******";

// Create connection
$conn = new mysqli($servername, $username, $password,"*****");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name=$_POST['name'];
//$file=$_FILES['userfile'];
if(isset($_POST['submit']))
{
if($name!=""){
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
$insert="Insert into photograph_submission(Name,Photograph,names) values('".$name."','".$imgData."', '".$_FILES['userfile']['name']."')";
header('Location: thank.html');
if($conn->query($insert)===FALSE){
echo "Error: " . $insert . "<br>" . $conn->error;
}
echo "<meta http-equiv='refresh' content='0'>";
}
else{

$message = "Please enter all * marked details..\\nTry again.";
echo "<script type='text/javascript'>alert('$message'); window.location = 'http://reflux.in/photography.html';</script>";
echo "<meta http-equiv='refresh' content='0'>";

}
}
//echo"<h1>Seems you have not entered all details<a href='http://reflux.in/photography.html'>Click to submit again</a></h1>";
?>

通过这个我试图显示图像......它存储在display.php中:

<?php
$servername = "***********";
$username = "*************";
$password = "**********";

// Create connection
$conn = new mysqli($servername, $username, $password,"u932729557_main");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$insert="Select Photograph from photograph_submission where Name='Umang Bajaj'";
if($conn->query($insert)===FALSE){
echo "Error: " . $insert . "<br>" . $conn->error;
}
$result = $conn->query($insert);
header("data:image/png;base64");
$row= $result->fetch_assoc();
echo base64_encode( $row['Photograph'] );
// echo '<img src="data:image/jpg;base64,'.base64_encode( $row['Photograph'] ).'"/>';
?>

当我打开 refux.in/display.php 时,它不显示图像...

最佳答案

我将向您展示创建 uploadImage 和 getImage 的示例

上传图片文件

<?php
$target_dir = "uploadSlike/";
$target_file = $target_dir . basename($_FILES["UploadSlike"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["UploadSlike"]["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["UploadSlike"]["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["UploadSlike"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["UploadSlike"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

获取图像

<?php

$id = $_GET['id'];
// do some validation here to ensure id is safe

$link = mysql_connect("localhost", "alex", "alex");
mysql_select_db("alex");
$sql = "SELECT avatar FROM users WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);

header("Content-type: image/jpeg");
echo $row['avatar'];
?>

然后就可以在 $_SESSION['userid'] 的页面中调用

<div class="avatar"><img src="img/'.$image.'" />';</div>

关于php - 在数据库中插入图像文件,然后使用php显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48365319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com