【问题标题】:Can't display image after uploading it to a data base, PHP and Mysql将图像上传到数据库,PHP和Mysql后无法显示
【发布时间】:2014-06-18 15:09:24
【问题描述】:

我需要为客户制作一个网页,以将图像上传到数据库并显示它们。

我可以将图像上传到数据库中,但我无法显示它们,但我不知道为什么

这是我的代码:

<!DOCTYPE html>
    <head>
    <body>

    <form action="form.php" method="post" enctype="multipart/form-data">
        File:
        <input type="file" name="image" /> <input type="submit" value="Upload" />
    </form>

    <?php 
        mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("test" ) or die(mysql_error());

        $file = $_FILES['image'] ['tmp_name'];

        if (!isset($file)) {
            echo "<br>Please select an image.";
        }
        else {
            $image = addslashes(file_get_contents($_FILES['image'] ['tmp_name']));
            $imageName = addslashes($_FILES['image']['name']);
            $imageSize = getimagesize($_FILES['image']['tmp_name']);

            if ($imageSize == FALSE)
                echo "<br><br>Thats not an image. <br><br>";


            else{
                if (!$insert = mysql_query("INSERT INTO imgup VALUES ('','$imageName','$image')"))
                    echo "Problem uploading the image.";
                else{
                    $lastId = mysql_insert_id();
                    echo "Article uploaded.<p /> Your image:<p /> <img src=get.php?id=$lastId>";
                }
            }
        }

        ?>

    </body>
</html>

这是我将图像 blob 转换为图像的文件:

<?php 
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("test" ) or die(mysql_error());

        $id = addslashes($_REQUEST['id']);


        $image = mysql_query("SELECT * FROM blog WHERE id=$id");
        $image = mysql_fetch_assoc($image);
        $image = $image['image'];

        header("Content-type: image/jpeg");

        echo $image;

?>

最后图像不显示,这就是我得到的:http://goo.gl/gi1Uuc

如果我去检查我的数据库,图像已经成功上传......

【问题讨论】:

    标签: php database image upload


    【解决方案1】:

    根据文件使用内联 base64 编码。这是通过以下方式完成的:

    echo '<img src="data:image/jpeg;base64,'.base64_encode( $image ).'"/>';
    

    字体:base64_encode

    T大写(类型),因为在IE中会报错。尝试使用函数 file_get_contents 进行打印。

    header('Content-Type: image/jpeg');
    echo readfile($image);
    

    【讨论】:

    • @user3479756 检查您的 BLOB 记录没有损坏。
    • @user3479756 这是JPEG?还是 PNG?
    • 我该怎么做?我只尝试使用 jpg 文件,你知道有什么其他方法可以达到同样的效果吗?我的目的是创建一个“博客”,以便人们可以将文章上传到他们的网页(我也会这样做)
    • @user3479756 解决这个问题的一种方法是在将文件放入 blob 之前创建文件的校验和,以便以后能够验证数据完整性,但最好的方法是保留目录中的文件并仅保存路径或名称。
    • 好吧,我尝试了 readfile,但没有任何反应,我想我会尝试使用路径,你知道任何教程吗?导致我在这里有点迷路,我以前也想过,但我只是不知道如何让 php 知道文件已上传,以便 php 可以存储新文件的路径,我不知道知道我是否说清楚了
    【解决方案2】:

    我不会在数据库中存储任何图像。您应该将其保存为文件,并将文件名存储在数据库中。然后,您可以配置从哪个目录获取图像,而无需担心图像的完整路径,或将二进制数据存储在您的数据库中(糟糕)。

    【讨论】:

    • 嗨瑞恩,我以前想过,但我不知道怎么做,你知道怎么做的教程吗?不胜感激,谢谢
    • 我不知道任何教程,但我会试一试。
    • 假设有人尝试上传文件,而您的网站空间的路径是 /path/to/document_root。您想在 move_uploaded_file($_FILES['file'], '/path/to/document_root/uploaded/'.$uploaded_file_name) 之类的内容中使用 move_uploaded_file。然后当你访问它时,你想在前端使用它,你通过'yourdomain.com/uploaded/'.$uploaded_file_name引用它。您不想要数据库中不可索引的任何内容。您需要对数据库中的二进制数据的可索引引用。因此,您将 $uploaded_file_name 存储在列中,并将路径和 url 存储在脚本中
    • 我的意思是 $_FILES['file']['tmp_name'],而不是 $_FILES['file']。
    【解决方案3】:

    尝试改变:

    $image = mysql_query("SELECT * FROM blog WHERE id=$id");
    

    到:

    $image = mysql_query("SELECT * FROM blog WHERE id = '$id'");
    

    【讨论】:

      【解决方案4】:

      使用addslashes 转义图像文件可能会损坏它,imagesize 测试应该足够了

      【讨论】:

      • 嘿!感谢您的建议,不幸的是,如果我删除了图像的附加斜线,则不要上传并将“上传图像的问题”发回给我。错误
      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2015-01-01
      • 1970-01-01
      相关资源
      最近更新 更多