【问题标题】:(PHP) The Image cannot be displayed because it contains errors(PHP) 图片无法显示,因为它包含错误
【发布时间】:2012-04-30 13:46:19
【问题描述】:

我已经阅读了几乎所有与此相关的主题。我不知道是什么原因造成的。

这是显示图片的代码

<html>
<body>
<?php 

                    mysql_connect('localhost','root','') or die("Unable to Connect: ".mysql_error());
                    mysql_select_db("punjabi") or die("Unable to Select Database: ".mysql_error());

                    $sql = "SELECT imagename, mimetype, imagedata
                            FROM images WHERE ID = 1";

                    $result = @mysql_query($sql);
                    if(!$result)
                    {
                        die(mysql_error());
                    }

                    $file = mysql_fetch_array($result);

                    $imagename = $file['imagename'];
                    $mimetype = $file['mimetype'];
                    $imagedata = $file['imagedata'];

                    header("content-type: $mimetype");

                    echo($imagedata);

?>

这是插入图片的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Post</title>
<style type="text/css">
    *
    {
        margin:0px;
        padding:0px;
    }
</style>

</head>

<body bgcolor="#333333">

<?php if(isset($_POST['submit'])):
        {


            if (!is_uploaded_file($_FILES['uploadfile']['tmp_name']))
            {
                die("$uploadfile is not an uploaded file!");
            }

            $uploadfile = $_FILES['uploadfile']['tmp_name'];
            $uploadname = $_FILES['uploadfile']['name'];
            $uploadtype = $_FILES['uploadfile']['type'];
            $uploaddesc = $_POST['desc'];


            $tempfile = fopen($uploadfile,'rb');
            $filedata = fread($tempfile,filesize($uploadfile));
            $filedata = addslashes($filedata);
            $sql = "INSERT INTO images SET
            imagename = '$uploadname',
            mimetype = '$uploadtype',
            description = '$uploaddesc',
            imagedata = '$filedata'";
            $ok = @mysql_query($sql);
            if (!$ok) die("Database error storing file: " .mysql_error());

            $sql = "Select id from images where imagename = '$uploadname'";
            $result = @mysql_query($sql);
            if(!$result) die(mysql_error());
            if(mysql_num_rows($result)!=1) die(mysql_error());

            $row = mysql_fetch_array($result);
            $imageid = $row['id'];

            $title = $_POST['title'];
            $content = $_POST['content'];

            $sql = "INSERT into posts SET
                    title = '$title',
                    content = '$content',
                    imageid = '$imageid'";
            if(!@mysql_query($sql))
            {
                die(mysql_error());
            }


            header("Location:http://localhost/punjabi/adminhome.php");  

        }


?>

<?php else: ?>

    <div id="post">
        <form action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post" enctype="multipart/form-data" >
            Title: <input type="text" name="title" /><br /><br /><br />
            Content:<br /> <textarea cols=100 rows="40" wrap="hard" name="content" /></textarea><br /><br />
            Image: <input type="file" name="uploadfile" /><br /><br />
            Image Desc: <input type="text" name="desc" /><br /><br />
            <input type="submit" value="Submit" name="submit" />
        </form>
    </div>

<?php endif; ?>
</body>
</html>

这是表结构:

CREATE TABLE filestore (
-> ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
-> FileName VARCHAR(255) NOT NULL,
-> MimeType VARCHAR(50) NOT NULL,
-> Description VARCHAR(255) NOT NULL,
-> FileData MEDIUMBLOB
->);

我们将不胜感激!

【问题讨论】:

  • 为什么不使用参数化插入查询而不是addslashes()?我认为此功能会修改您的内容。
  • 在浏览器中直接打开生成图片.php-script看到Warning: Cannot modify header information - headers already sent by...了吗?
  • 当心将未经处理的$_POSTs 直接插入到您的查询中。 SQL注入是个问题!

标签: php image show


【解决方案1】:

问题是当你这样做时标头已经发送:

<html>
<body>
<?php 

所以你不能再为图片设置标题了:

header("content-type: $mimetype");

在 php 标签解决这个问题之前,只需去掉 html(图像不是 html/文本)。

【讨论】:

  • 谢谢先生:D 我删除了 html 和 body 标签,它工作了:D
  • 但我仍然很困惑...我在 html 文档中使用显示图像代码...那么我应该如何解决它?
  • @AntiSaby 代码的第一部分(没有 html)将图像发送到浏览器,因此要在 html 文档中使用它,您必须使用图像标签:&lt;img src="/your/script.php"&gt;
  • @AntiSaby 没问题,如果它适合您,请不要忘记将答案标记为已接受(复选标记)。
猜你喜欢
  • 1970-01-01
  • 2020-03-23
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
相关资源
最近更新 更多