【问题标题】:how do I display on my webpage an image file stored in my mysql database?如何在我的网页上显示存储在我的 mysql 数据库中的图像文件?
【发布时间】:2017-02-01 06:35:23
【问题描述】:

有人可以帮忙吗,因为我不知所措。如何从 MYSQL 数据库中获取图像到我的网页上。 anyname.html

数据库名称是:上传 表名是:images

id 主要 int(11) AUTO_INCREMENT
名称 varchar(100)

大小 int(11)

输入 varchar(20)

内容媒体块

上传效果很好,但无法让它显示能够在 html 页面中使用它的图像。当我上传图片时,我得到了回报:存储了 11 号图片但我没有看到图片:这是我的代码,所以请有人帮忙,因为我已经做了很多年了,但没有结果。

我的upload.php代码:

<?php

// Check for post data.
if ($_POST && !empty($_FILES)) {
$formOk = true;

//Assign Variables
$path = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];

if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
    $formOk = false;
    echo "Error: Error in uploading file. Please try again.";
}

//check file extension
if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif'))) {
    $formOk = false;
    echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
}
// check for file size.
if ($formOk && filesize($path) > 500000) {
    $formOk = false;
    echo "Error: File size must be less than 500 KB.";
}

if ($formOk) {
    // read file contents
    $content = file_get_contents($path);

    //connect to mysql database
    if ($conn = mysqli_connect('localhost', 'root', '', 'upload')) {
        $content = mysqli_real_escape_string($conn, $content);
        $sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";

        if (mysqli_query($conn, $sql)) {
            $uploadOk = true;
            $imageId = mysqli_insert_id($conn);
        } else {
            echo "Error: Could not save the data to mysql database. Please try again.";
        }

        mysqli_close($conn);
    } else {
        echo "Error: Could not connect to mysql database. Please try   again.";
        }
    }
}
?>

<html>
<head>
    <title>Upload image to mysql database.</title>
    <style type="text/css">
        img{
            margin: .2em;
            border: 1px solid #555;
            padding: .2em;
            vertical-align: top;
        }
    </style>
</head>
<body>
    <?php if (!empty($uploadOk)): ?>
        <div>
            <h3>Your Image has been Uploaded:</h3>
        </div>
        <div>
            <img src="image.php?id=<?=$imageId ?>" width="300px" height="300px"></img>
            <strong><br /><br />Embed</strong>: <input size="30" value='<img src="image.php?id=<?=$imageId ?>">'>
        </div>

        <hr>
    <?php endif; ?>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" >
      <div>
        <h3>Image Upload:</h3>
      </div>
      <div>
        <label>IMAGE SELECT<br /></label>
        <input type="hidden" name="MAX_FILE_SIZE" value="500000">
        <input type="file" name="image" />
        <br /><br />
        <input name="submit" type="submit" value="Upload Image">
      </div>
    </form>
</body>
</html>

【问题讨论】:

  • 不要将文件保存在数据库中。 stackoverflow.com/a/38829952/267540
  • 你为什么要把图像数据放到数据库中呢?这通常最好保存在文件系统中。既然你调用 image.php 来显示图像,你不觉得 that 脚本的代码可能更相关吗……?无论如何,直接在浏览器中使用相同的参数调用该脚本(并注释掉之前该脚本中的任何 header 调用),然后检查结果。

标签: php html mysql image


【解决方案1】:

这似乎与那里的问题重复了PHP display image BLOB from MySQL

希望对你有帮助。

【讨论】:

    【解决方案2】:

    如果将图像数据存储在数据库中,请使用下一个image.php:

    <?php
    
      $id = $_GET['id'];
    
      $link = mysql_connect('localhost', 'root', '');
      mysql_select_db('upload');
      $sql = "SELECT content FROM images WHERE id=$id";
      $result = mysql_query("$sql");
      $row = mysql_fetch_assoc($result);
      mysql_close($link);
    
      header("Content-type: image/jpeg");
      echo $row['content'];
    ?>
    

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 2013-02-09
      • 2021-11-16
      • 2012-10-24
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多