无法发送PHP标头?

时间:2013-11-11 06:58:56

标签: php curl header http-headers

所以,快速提问大家......我有一个生成编辑过的图像的PHP文件。该文件如下所示。它从服务器抓取图像并按特定规格裁剪它。

<?php
header("Content-Type: image/jpg");

//error_reporting(E_ALL);
//ini_set('display_errors', '1');

$src = getcwd()."/".$_GET["img"].".jpg";
$screenw = $_GET["sw"];
$screenh = $_GET["sh"];


$jpeg_quality = 100;

$w = $screenw;
$h = 428;

$targ_w = $w;
$targ_h = $h;

$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor($targ_w, $targ_h);

$x = 1000 - floor($screenw / 2);
$y = 1000 - floor(428 / 2);

imagecopyresampled($dst_r, $img_r, 0, 0, $x, $y, $targ_w, $targ_h, $w, $h);

imagejpeg($dst_r, null, 100);
exit;
?>

该文件在我的本地MAMP服务器上运行正常。但是,当我将其上传到000webhost时,我立即得到了一个

  

无法显示图像image1,因为它包含错误

消息。并且,经过一些测试,我认为我的标题是它的原因。如果我卷曲它,实时网站的结果是:

Date: Mon, 11 Nov 2013 06:47:40 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Connection: close
Content-Type: image/jpeg

和localhost:

Date: Mon, 11 Nov 2013 06:52:20 GMT
Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8y DAV/2 PHP/5.4.10
X-Powered-By: PHP/5.4.10
Content-Type: image/jpeg

我看到连接在实时站点上发送标头之前关闭,因此抛出错误......为什么会发生这种情况?这是我的代码的问题吗?这可以用PHP解决吗?请告诉我。提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

我意识到这是000webhost的一个问题..他们不仅会在页面末尾添加跟踪代码,而且还会在文件顶部发送换行符。这是在发送其他标头之前发送标头,因此将其添加到htaccess文件更正了问题:

php_value auto_append_file none
php_value auto_prepend_file none

不管怎样,不管怎么样。

相关问题