dops57958 2012-04-30 13:46 采纳率: 0%
浏览 63

(PHP)无法显示图像,因为它包含错误

I've read almost all topics related to this. i dont know whats causing it.

This is code for showing images

<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);

?>

This is the code for inserting images

<!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>

And This is the Table Structure:

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
->);

And Help would be appreciated!

  • 写回答

1条回答 默认 最新

  • dreinuqm992401 2012-04-30 13:48
    关注

    The problem is that the headers have already been sent when you do:

    <html>
    <body>
    <?php 
    

    So you cannot set the header for the image anymore:

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

    Just getting rid of the html (an image is not html / text) before the php tag should solve that.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?