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

标签 php html mysql

我正在尝试使用 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/

相关文章:

如果文本已经输入,JavaScript 不会复制 innerHTML

php - 建议使用 ISO-8859-1 或 UTF-8 构建网站

php - 将数字转换为字符串

php - 从数据库填充的菜单

javascript - 使下拉框适合在没有相对位置的包装内

html - 无法在页脚内内联安排两个 Bootstrap 的按钮

java - 显示 NumberFormatException

sql - 做这些方式之一的优点或缺点是什么

PHP如何将斜杠添加到数组中

php - MySQL:我需要根据相同的条件将多行中的相同列名拉到单个查询中