Apache 或 PHP 生成前置换行符 [英] Apache or PHP generating prepending line feed character

查看:27
本文介绍了Apache 或 PHP 生成前置换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 PHP Web 应用程序中生成 XML 文件:

I am trying to generate an XML file in a PHP web application:

<?php
... 
header('Content-Type: application/xml');
header('Content-Disposition: attachment; filename=test.xml');
echo "<?xml version=\"1.0\"?>\r\n" . 
...

奇怪的是,当使用我的服务器时(分别是 PHP 版本 5.3.8/Apache 2.2.17 和 PHP 版本 5.3.10-1/Apache 2.2.22)换行(十六进制 0a) 字符插入到输出的开头,导致无法使用的无效 XML.还有 在线关于这个问题,未解决.

Bizarrely, when using my servers (PHP Version 5.3.8/Apache 2.2.17 and PHP Version 5.3.10-1/Apache 2.2.22 respectively) a line feed (hex 0a) character is inserted in the beginning of the output, resulting in invalid XML that cannot be used. There's one more online question about this, unresolved.

所以如果我尝试 echo "bug"; 我得到 4 个字节,而不是 3 个:0a 62 75 67

So if I try echo "bug"; I get 4 bytes, not 3: 0a 62 75 67

但是,在本地使用 WAMP 服务器(PHP 5.4.3/Apache 2.4.2)时,我得到 3 个字节:62 75 67.

However, when using WAMP server locally (PHP 5.4.3/Apache 2.4.2), I get 3 bytes: 62 75 67.

  • 这是已知错误/功能吗?
  • 这是配置问题吗?
  • 应该归咎于 Apache 还是PHP?
  • 我需要升级我的服务器吗?我宁愿不要.

推荐答案

0a 问题似乎是由我的主要 PHP 文件中包含的 PHP 文件中的尾随 Enter 字符引起的.当我从包含文件中删除它时,输出中的 0a 字符消失了.

It seems like the 0a problem was caused by a trailing Enter character in a PHP file included by my main PHP file. When I removed it from the include file, the 0a character in my output disappeared.

我觉得这件事的奇特之处在于我经历过的 PHP 版本之间对空格的不同处理,以及在测试社区的建议时我仍然得到 0a 的事实.

What I find peculiar about this is the different handling of whitespace between PHP versions that I experienced, and the fact that I still got the 0a when testing the community's suggestions.

我没有更多时间对此进行研究,但我对遇到类似问题的人的建议是检查包含文件中的空格是否可能会影响等式.此外,避免按照下面 Dan 的建议结束 <?php 标签.

I have no more time to put research into this, but my advice to people experiencing similar problems is to check whether whitespace in include files may play into the equation. In addition, avoid ending the <?php tag as suggested by Dan below.

这篇关于Apache 或 PHP 生成前置换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆