使用<img/>标记调用PHP脚本以显示从数据库

时间:2018-04-28 17:37:11

标签: php html image

我的PHP脚本,用于将编码图像数据作为“blob”类型存储在数据库表中:

include './libraries/LIB_mysql.php'; # library for using the 'insert()' function at the end
$imageFilePath = "./pythonProgrammingProgram.PNG";
$imageFileRead = file_get_contents($imageFilePath); # == �PNG  IHDR�A ... uY�IEND�B`� (... and so on)
$imageEncode = base64_encode($imageFileRead); # iVBORw0KGgoAAAA (... and so on)
$data_array['image_id'] = 3;
$data_array['image_file'] = $imageEncode;
$table = 'images';
insert(DATABASE, $table, $data_array);

图像数据似乎正确存储在数据库中:

mysql> select * from images;
| image_id | image_file
|        3 | iVBORw0KGgoAAAA # (... and so on)

然后我希望使用'img'标记在HTML页面中显示图像,该标记调用PHP脚本:<img src="./imageDisplay.php?pictureID=3">。 PHP脚本imageDisplay.php从数据库中检索图像数据并回显解码图像:

header("Content-type: image/png");
include('./libraries/LIB_mysql.php'); # for using exe_sql() function
$image_id = $_GET['pictureID']; # == 3 -> corresponds to the value in the database !!
$sql = "SELECT image_file FROM images WHERE image_id = '" . $image_id . "'"; # $image_id";
list($img) = exe_sql(DATABASE, $sql); # $img contains the decoded data: == �PNG  IHDR�A ... uY�IEND�B`� (... and so on)
echo base64_decode($img);
exit;

在localhost上加载HTML文件时,我看到了损坏的图像。在网络下的Firefox中 - &gt;响应我看到了:

Name: imageDisplay.php
Dimensions: 0 × 0
MIME Type: image/png

我猜图像数据的传输不起作用,但我不明白为什么。 base64_decode($img)中的解码图像数据与编码前$imageFileRead中的相同,因此我猜数据库操作不是问题,而是echo base64_decode($img);<img src="./imageDisplay.php?pictureID=3">中的某个位置。找到了这个(问题)[PHP - call a function from a img src和(另一个)[The image cannot be displayed because it contains errors但仍然无法看到如何修复它: - (

3 个答案:

答案 0 :(得分:0)

我会认真重新考虑在数据库中存储图片。最好的方法是保存文件并在数据库中包含图像路径。如果绝对需要,请尝试使用以下代码保存:

$sql = "select name from images where id=1";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);

$image = $row['name'];
$image_src = "upload/".$image;

?>
<img src='<?php echo $image_src;  ?>' >

此代码显示

{{1}}

此代码来自此示例 - http://makitweb.com/upload-and-store-an-image-in-the-database-with-php/#base64

您需要修改它才能使用您的代码。

答案 1 :(得分:0)

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" accept="png/jpeg/jpg/image"><br><br>
<input type="submit" value="submit" name="submit">
</form>
<?php 
 if(isset($_POST['submit'])){`enter code here`
    $name       = $_FILES['file']['name'];  `enter code here`
    $temp_name  = $_FILES['file']['tmp_name'];  
     // $allowed = array("pdf" => "pdf");
     // var_dump($name,$temp_name);
    if(isset($name)){
        if(!empty($name)){      
            $location = './uploads1/';      
            if(move_uploaded_file($temp_name, $location.$name)){
            //var_dump($temp_name,$location.$name);
                echo 'File uploaded successfully';

                $con=mysqli_connect("localhost","root","");
echo "connect";
if(!$con)
{
die("not connect");
}
mysqli_select_db($con,"dstpurse"); 


$query="INSERT INTO `image_upload`(`name`,`path`) VALUES('$name','$location')";

$result=mysqli_query($con,$query);
if($result)
{
echo "inserted";
}
else
{
echo "not inserted";
}

            }
        }       
    }  else {
        echo 'You should select a file to upload !!';
    }
    $query1 = "SELECT * FROM `image_upload`";
$result1 = mysqli_query($con,$query1) or die("query failed:".mysqli_error());
while($row=mysqli_fetch_array($result1))
{
var_dump($name); 

echo "<a href=".$location.$name.">dowenload</a>";

}


}

?>

答案 2 :(得分:-1)

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" accept="png/jpeg/jpg/image"><br><br>
<input type="submit" value="submit" name="submit">
</form>
<?php 
 if(isset($_POST['submit'])){`enter code here`
    $name       = $_FILES['file']['name'];  `enter code here`
    $temp_name  = $_FILES['file']['tmp_name'];  
     // $allowed = array("pdf" => "pdf");
     // var_dump($name,$temp_name);
    if(isset($name)){
        if(!empty($name)){      
            $location = './uploads1/';      
            if(move_uploaded_file($temp_name, $location.$name)){
            //var_dump($temp_name,$location.$name);
                echo 'File uploaded successfully';

                $con=mysqli_connect("localhost","root","");
echo "connect";
if(!$con)
{
die("not connect");
}
mysqli_select_db($con,"dstpurse"); 


$query="INSERT INTO `image_upload`(`name`,`path`) VALUES('$name','$location')";

$result=mysqli_query($con,$query);
if($result)
{
echo "inserted";
}
else
{
echo "not inserted";
}

            }
        }       
    }  else {
        echo 'You should select a file to upload !!';
    }
    $query1 = "SELECT * FROM `image_upload`";
$result1 = mysqli_query($con,$query1) or die("query failed:".mysqli_error());
while($row=mysqli_fetch_array($result1))
{
var_dump($name); 

echo "<a href=".$location.$name.">dowenload</a>";

}


}

?>