使用pdo显示数据库中的图像,但它只显示损坏的图像

时间:2017-05-14 23:44:39

标签: php database pdo

enter image description here

<form action='upload.php' method='post' enctype="multipart/form-data">
    <input type='text' required="required" name='activityName' placeholder="Name of the activity..."><br><br>
    <input type='date' required="required" name='date'><br><br>
    <input type='file' required="required" name='file'><br><br>
    <button type='submit' name='submit'>Upload</button>
</form>
<?php

$ps = $pdo->query("SELECT * FROM activities");

echo "<br>";
echo "<table>";
while($row = $ps->fetch(PDO::FETCH_BOTH)){
    echo "<tr>";
    echo "<td>";?> <img src="<?php echo $row['image'];?>" height="100" width="100"> <?php echo "</td>";
    echo "<td>"; echo $row['name']; echo "</td>";
    echo "</table>";
}
?>

以下代码是我用来上传到数据库的代码..

<?php

include 'dbh.php';

if (isset($_POST['submit'])){

    $file = $_FILES['file'];

    $fileName = $_FILES['file']['name'];
    $fileTmp = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
    $activityName = $_POST['activityName'];
    $activityDate = $_POST['date'];



    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allow = array('jpeg', 'jpg', 'png');

    if (in_array($fileActualExt, $allow)){

        if($fileError === 0){
            if ($fileSize < 1000000){
                $fileNewName = uniqid('', true) . '.' . $fileActualExt;
                $fileDestination = '/home/bh03te/public_html/webproject/uploads/' . $fileNewName;
                move_uploaded_file($fileTmp, $fileDestination);

                $sql = "INSERT INTO activities (name, date, image) VALUES ('$activityName', $activityDate, '$fileNewName')";
                $result = $pdo->query($sql); 

                header("Location: activities.php?uploadsuccess");
            } else{
                echo "This file is too large";
            }
        } else{
            echo "There was an error uploading this file";
        }

    } else{
        echo "You cannot upload files of this type!";
    }

}

这是我所拥有的所有代码,并且不明白为什么它不起作用。我是否需要将数据库中的数据类型作为特定的内容?它目前只是VARCHAR。我不知道它是否会有所作为。数据库中的所有名称和所有内容都是正确的,因为图像会上传,但它们只是没有正确回显。它只输出一张破碎的图像。

2 个答案:

答案 0 :(得分:1)

我假设您的文件已经正确上传,我发现您没有在图像数据库中保存图像的整个路径,因此您还需要将其写入图像的src属性中在文件名之前(带有扩展名)。

打印时可以这样做:

// I am deleting everything up to public_html because I suppose that's the DOCUMENT_ROOT, so that's not web accessible
<img src="/webproject/uploads/<?php echo $row['image'];?>" height="100" width="100">

或者在插入数据库时​​:

// I am deleting everything up to public_html because I suppose that's the DOCUMENT_ROOT, so that's not web accessible
$path = '/webproject/uploads/'.$fileNewName;
$sql = "INSERT INTO activities (name, date, image) VALUES ('$activityName', $activityDate, '$path')";

答案 1 :(得分:0)

您应该保存原始图像数据及其mime类型,然后创建一个php文件,在回显原始图像数据之前将标头设置为正确的contentType。

E.g。

<img src="getImg.php" height="100" width="100" />

PHP:

<?php header("contentType:".$mime); echo $raw_img; ?>