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

时间:2018-01-21 09:15:09

标签: 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时,它不会显示图像......

1 个答案:

答案 0 :(得分:0)

我将向您展示创建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.";
    }
}
?>

的getImage

<?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>