I'm using php to force the download of .bz2 archives and .deb files for a webservice I'm making, but after downloading the files are invalid. I opened both the original file and the file downloaded through php in a text editor and I noticed that the one downloaded with php had 1 extra space at the beginning of the file, after removing that space the files did work. What could be causing this?
here is the code I'm using:
header('Content-Type: package/x-generic');
header('Content-Length: ' . filesize($file));
ob_clean();
ob_flush();
readfile($file);
exit;
I already tried experimenting with different headers and the clean and flush functions.
Any help or ideas will be greatly appreciated.
Probably because your PHP file(s) are saved as UTF-8 with a BOM (Byte Order Mark). Try saving your php files as UTF-8 without a BOM. Any reasonably advanced text-editor will allow you to save files as UTF-8 without a BOM.
Also see my answer to another question that is related to this. It also mentions what arnaud576875 talks about as being a possible cause of the problem.
You may have an extra space after a ?> in some of you php files. It is generally a good idea to omit closing ?> tags to avoid this problem.
If that's the case you could use headers_sent() after flush() to know from which file/line the extra space comes from.
If you have whitespace before a php tag that could cause the space at the beginning. If you need output buffering (ob_clean(), ob_flush()) chances are there is a space or data getting output somewhere prior to setting the headers. Turn off output buffering if possible and you should get an error telling what line the output started.
I finally found the solution to my problem, after a long search I found out that I was including another php file into my php file, that file had an space behind the ?> tag. :S Thanks to everyone who answered
Related
I am sending an AJAX request expecting JSON response.
However, the returned JSON is preceded with a red dot\bullet which is causing a parse error.
Here is a screenshot from Postman:
The dot is not shown on Raw or Preview display, only on Pretty.
In Chrome Dev Tools Network tab it appears under Response. Preview is shown normally as if the dot isn't there.
As mentioned in a comment before: In Chrome, red dots usually represent non-printable special unicode characters.
Please check your server side code to prevent outputting those characters
If your files are encoding with UTF-8, better to encode them with UTF-8 without BOM. This can be easily done through notepad++. The steps are as follows,
Open your files in notepad++.
Go to Encoding option on the file menu.
Then select the option "convert to UTF-8 without BOM".
This may resolve your problem.
You need to clear you object buffer on server side.
I am using PHP as my server side language and I faced similar problem and the solution was cleaning my buffer using ob_clean();
I was faced red dots problem in my ajax response I tried a lot of solution but doest work for me after then I tried ob_clean() function I got the solution
I solved my problem using ob_clean() inside the constructor method
function __construct()
{
ob_clean();
}
i had the same problem and fixed this by converting the file from utf-8 to utf-8 without BOM
I'm having troubles with multiple html pages include.
I had main index.html but it started to be a bit too complex so I decided to split it in multiple html files and each one of them inport into single index.php using php.
index.php file
<?php
include('head.html');
include('header.html');
include('slideshow.html');
include('pictureGalery.html');
include('footer.html');
include('closer.html');
?>
using Google Chrome Developer Tool I found, that php includes included also some white spaces (you can see them in picture in between of divs header, content, container etc...
With a bit of googling I found some arciles about this problem, like:
PHP include causes white space at the top of the page (I can't use edit cmd command because I have win7 64-bit and it is not implemented there. You can only use "open notepad " command, but these whitespaces are not visible in notepad.
UTF-8 Without BOM?
PHP include() before doctype, causing a white space
I also tried to import reset.css file (http://meyerweb.com/eric/tools/css/reset/) but it didnt work either. The only way that seems to work for me is one that posted cebasso (https://stackoverflow.com/a/14362246/1784053). But I find this way a bit too agresive and unefective.
Any other ideas how to fix this problem?
Then, ends up this is caused by unexpected BOM on the file that are included:
For future reference, I just post the code used to check by PHP plainly.
print_r(array_map('dechex', array_map('ord', str_split(file_get_contents("xxxxxxxxxxxxx.html")))));
I would definitely prefer using Notepad++ or other software to save a non-BOM version,
but if still prefer using PHP to remove them, just use:
This removes first 3 characters in the file PERMANENTLY every time you execute it, so run once only.
$filename="xxxxxxx.html";
$filec = file_get_contents($filename);
file_put_contents($filename,substr($filec, 3, strlen($filec)));
Hopes it helps :D
Good luck!
I have the same problem before a moment. but when I see BOM characters, I think of a solution. Am not sure, but these characters for utf-8 encoding. I save my included file as ansi and the problems solved. also it's works fine with encode in utf-8 without BOM in Notepad++
Thanx
Regards
I'd look closely at your included files. Ensure that there is no white space at the top or bottom of every one of those documents. White space is not ignored by php and will definitely be included.
I have encountered a similar problem described here (and in other places) -
where as on an ajax callback I get a xmlhttp.responseText that seems ok (when I alert it - it shows the right text) - but when using an 'if' statement to compare it to the string - it returns false.
(I am also the one who wrote the server-side code returning that string) - after much studying the string - I've discovered that the string had an "invisible character" as its first character. A character that was not shown. If I copied it to Notepad - then deleted the first character - it won't delete until pressing Delete again.
I did a charCodeAt(0) for the returned string in xmlhttp.responseText. And it returned 65279.
Googling it reveals that it is some sort of a UTF-8 control character that is supposed to set "big-endian" or "small-endian" encoding.
So, now I know the cause of the problem - but... why does that character is being echoed?
In the source php I simply use
echo 'the string'...
and it apparently somehow outputs [chr(65279)]the string...
Why? And how can I avoid it?
To conclude, and specify the solution:
Windows Notepad adds the BOM character (the 3 bytes: EF BB BF) to files saved with utf-8 encoding.
PHP doesn't seem to be bothered by it - unless you include one php file into another -
then things get messy and strings gets displayed with character(65279) prepended to them.
You can edit the file with another text editor such as Notepad++ and use the encoding
"Encode in UTF-8 without BOM",
and this seems to fix the problem.
Also, you can save the other php file with ANSI encoding in notepad - and this also seem to work (that is, in case you actually don't use any extended characters in the file, I guess...)
If you want to print a string that contains the ZERO WIDTH NO-BREAK SPACE char (e.g., by including an external non-PHP file), try the following code:
echo preg_replace("/\xEF\xBB\xBF/", "", $string);
If you are using Linux or Mac, here is an elegant solution to get rid of the character in PHP.
If you are using WordPress (25% of Internet websites are powered by WordPress), the chances are that a plugin or the active theme are introducing the BOM character due a file that contains BOM (maybe that file was edited in Windows). If that's the case, go to your wp-content/themes/ folder and run the following command:
grep -rl $'\xEF\xBB\xBF' .
This will search for files with BOM. If you have .php results in the list, then do this:
Rename the file to something like filename.bom.bak.php
Open the file in your editor and copy the content in the clipbard.
Create a new file and paste the content from the clipboard.
Save the file with the original name filename.php
If you are dealing with this locally, then eventually you'd need to re-upload the new files to the server.
If you don't have results after running the grep command and you are using WordPress, then another place to check for BOM files is the /wp-content/plugins folder. Go there and run the command again. Alternatively, you can start deactivating all the plugins and then check if the problem is solved while you active the plugins again.
If you are not using WordPress, then go to the root of your project folder and run the command to find files with BOM. If any file is found, then run the four steps procedure described above.
You can also remove the character in javascript with:
myString = myString.replace(String.fromCharCode(65279), "" );
I had this problem and changed my encoding to utf-8 without bom, Ansi, etc with no luck. My problem was caused by using a php include function in the html body. Moving the include function to above my html (above !DOCTYPE tag) resolved the issue.
After I knew my issue I tested include, include_once and require functions. All attempts to include a file from within the html body created the extra miscellaneous 𐃁 character at the spot where the PHP code would start.
I also tried to assign the result of the include to a variable ... i.e $result = include("myfile.txt"); with the same extra character being added
Please note that moving the include above the HTML would not remove the extra character from showing, however it removes it from my data and out of the content area.
In addition to the above, I just had this issue when pulling some data from a MySQL database (charset is set to UTF-8) - the issue being the HTML tags, I allowed some basic ones like <p> and <a> when I displayed it on the page, I got the 𐃁 character looking through Dev Tools in Chrome.
So I removed the tags from the table and that removed the 𐃁 issue (and the blank line above the where the text was to be displayed.
I just wanted to add to this, since my Rep isn't high enough to actually comment on the answer.
EDIT: Using VIM I was able to remove the BOM with :set nobomb and you can confirm the presence of the BOM with :set bomb? which will display either bomb or nobomb
I use "Dreamweaver CC 2015", by default it has this option enabled: "include BOM signature" or something like that, when you click on save as option from file menu. In the window that apears, you can see "Unicode Options..". You can disable the BOM option. And remeber to change all your files like that. Or you can simply go to preferences and disable the BOM option and save all your files.
I'm using the PhpStorm IDE to develop php pages.
I had this problem and use this option of IDE to remove any BOM characters and problem solved:
File -> Remove BOM
Try to find options like this in your IDE.
Probably something on the server. If you know it's there, I would just bypass it until solved.
myString = myString.substring(1)
Chops off the first character.
When using atom it is a white space on the start of the document before <?php
A Linux solution to find and remove this character from a file is to use sed -i 's/\xEF\xBB\xBF//g' your-filename-here
My solution is create a php file with content:
<?php
header("Content-Type:text/html;charset=utf-8");
?>
Save it as ANSI, then other php file will require/include this before any html or php code
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
As a result,functions like session_start and setcookie can't run successfully,reporting:
Cannot modify header information -
headers already sent by
But the target file is like this:
1 <?php
2 session_start();
How to fix it?
FOUNDINGS
I've found the problem,the utf-8 formated files become utf-8+BOM after uploading to the server,so I've temporarily solved the problem by saving it as utf-8 again.
BUT,there are lots of other files with the same problem,how can I batch solve the issue?
Smells like a BOM problem as some editors don't show it. Try opening your file with Notepad++, Emacs or any other editor which show it. If you have some weird character before the <, you got the culprit.
If your file is Unicode-encoded, a two byte BOM (Byte Order Mark) is added at the start of the file. This BOM is not displayed by most editors. For example in Notepad++, you can change the encoding to UTF-8 without BOM or just a completely different encoding like ANSI.
One tip is to leave off the trailing ?> at the end of your PHP files – this avoids cases where you might have trailing whitespace at the end of the file.
There's only one way to fix it: find the space! It's somewhere, it's always somewhere.
To fix, try the three steps in this article.
can you post whole program what you tried? check this code,make sure you added ?> at end
<?php
session_start();
session_register("count");
if(!isset($_SESSION))
{
$_SESSION["count"]=0;
echo "<p>counter initialized</p>\n";
}
else
{
$_SESSION["count"]++;
}
echo "<p>The counter is now <b> $_SESSION[count]</b></p>";
echo "<p>Reload this page to increment</p>";
?>
Open the file in a good editor like vi and you will see all the nonsense characters and whitespace.
If you find the offending character and it is present many of your files you could probably use grep/xargs/sed to replace it out of all the files at once. Not sure how you do that on Windows though.
As to the answer suggesting that you leave off the closing ?> php tag... I've seen/heard this suggested several times but it just doesn't sit right with me. If you are going to omit the closing php tag from all of your files then you should put a comment at the end of every file indicating this so you know the file hasn't been truncated.
// end of file
However, if you are going to do this to all of your files you might as well just delete the whitespace after ?> tag. It's really not that hard unless you are sloppy with your coding.
Turn on Output buffering in PHP, this can be done at the php.ini level, or in .htaccess
http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering
Then all your output will be cached in a buffer until your script ends, and it's sent at once. This is often good from a performance perspective (as your server sends fewer, larger, chunks of data). Depending on your site architecture you may want to put a flush(); in after your HTML headers are generated.
I think i encountered a PHP bug
When my header file is in UTF-8 encoding and my index.php file is in ANSI PHP gives
" headers already sent " error .
Is this normal ? and if yes , can you explain why ?
Maybe your editor is writing a UTF-8 BOM to the beginning of the "header" file, and PHP, not knowing what the BOM is, considers it as data to output and does that annoying thing PHP does?
There's a long-standing WONTFIX bug on PHP's mis-handling of the BOM. Probably your only workaround is to find an editor that writes UTF-8 without it (which, actually, is most of them.)
I've seen this before. Somewhere outside of the the encoding is generating some whitespace. It was a pain and a half to track down.