我在使用PHP的头输出二进制数据时遇到问题,不知道为什么,你能帮忙吗?
(表格)
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
(PHP脚本)
<?php
require_once "connection.php";
$pic = $_FILES['fileToUpload'];
$pic_info = getimagesize($pic['tmp_name']);
$pic_mime_type = $pic_info['mime'];
$pic_data = file_get_contents($pic['tmp_name']);
$pic_size = $pic['size'];
header("Content-type: " . $pic_mime);
header("Content-length: " . $pic_size);
echo $pic_data;
?>
脚本的输出只是一个空的正方形,你能帮忙吗?
非常感谢!
您正在使用
header("Content-type: " . $pic_mime);
而不是
header("Content-type: " . $pic_mime_type);
并确保connection.php不会输出任何内容,因为它是在发送头之前解析的
http://www.php.net/manual/function.header.php
connection.php的示例
不良
<html>
<?php
echo 'foo';
?>
良好
<?php
$result = 'bar';
- 没有HTML代码(或在打开
<?php
标记之前的任何其他内容) - 没有
echo
(或print
、print_f
,或任何发送回数据的内容) - 没有关闭的
?>
标记(因此后面没有意外的空白)
还可以看看如何修复";标头已发送";PHP中的错误。