(PHP)无法显示图像,因为它包含错误
我读过几乎所有与此相关的主题。我不知道是什么引起的 这是显示图像的代码(PHP)无法显示图像,因为它包含错误,php,image,show,Php,Image,Show,我读过几乎所有与此相关的主题。我不知道是什么引起的 这是显示图像的代码 <html> <body> <?php mysql_connect('localhost','root','') or die("Unable to Connect: ".mysql_error()); mysql_select_db("punjabi") or die("Unable to Select Dat
<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>
我们将不胜感激 问题在于,在执行以下操作时,标题已经发送:
<html>
<body>
<?php
只要在php标记之前去掉html(图像不是html/文本)就可以解决这个问题。问题是,当您执行以下操作时,标题已经发送:
<html>
<body>
<?php
只要在php标记之前去掉html(图像不是html/文本)就可以解决这个问题。为什么不使用参数化的插入查询而不是
addslashes()
?我认为此函数会修改您的内容。当您在浏览器中直接打开generating image.php脚本时,是否看到警告:无法修改标题信息-标题已由…
发送?请注意将未初始化的$\u POST
直接插入您的查询中。SQL注入是个问题!为什么不使用参数化的插入查询而不是addslashes()
?我认为此函数会修改您的内容。当您在浏览器中直接打开generating image.php脚本时,是否看到警告:无法修改标题信息-标题已由…
发送?请注意将未初始化的$\u POST
直接插入您的查询中。SQL注入是个问题!谢谢,先生:D我删除了html和body标签,它成功了:D但我仍然很困惑…我在html文档中使用了show image代码…那么我应该如何解决它呢?@AntiSaby您的代码的第一部分(没有html)将图像发送到浏览器,以便在html文档中使用它,您必须使用图像标签:
@AntiSaby没问题,请不要忘记将答案标记为已接受(复选标记)如果它对你有用。谢谢先生:D我删除了html和body标签,它起作用了:D但我仍然很困惑…我在html文档中使用了show image代码…那么我应该如何解决它呢?@AntiSaby你的代码的第一部分(没有html)将图像发送到浏览器,以便在html文档中使用它,您必须使用图像标签:
@AntiSaby没问题,如果答案适合您,请不要忘记将其标记为已接受(复选标记)。
header("content-type: $mimetype");