I have a problem sending a file to client. I want to send plain text using header, but the out file, instead of having just my content, has two empty lines at the beginning. I don't know why this happens.
The variable I use is an xml format like this:
$section=<book>xbook<author>nmauthor</author></book>
I use this code to send the file.
header("Content-type:text/plain");
header("Content-Disposition: attachment;filename=file.xml");
header("Content-Transfer-Encoding:binary");
header("Pragma:no-cache");
header("Expires:0");
echo $section
I will be very grateful if someone helps me.
Could be many things... for one, if you have no space before your opening php tag, one thing to look at would be the file's encoding type. If you're using UTF-8, especially, make sure it is "without BOM" as that could cause your problem.
Related
In a webapp I place a <div id="xxx" contentEditable=true > for editing purpose. The encodeURIComponent(xxx.innerHTML) will be send via Ajax POST type to a server, where a PHP script creates a simple txt file from it which in turn can be downloaded from the user to store it locally or print it on screen. It works perfect so far, but … Yes, but, character encoding is a mess. All special characters like the german Ä are interpretated wrong. In this case as ä
I google for some days and I study PHP methods like iconv() and I know how to set up a browsers character encoding and also set a text editor for a correct correspondending decoding. But nothing helps, its still a messs, or becoming even weired.
So my question is : Where in this encoding/decoding roundtrip from the browser to a server and back to the browser I have to do what, to ensure that an Ä will still be an Ä ?
I answer my question, because it turns out to be another problem as stated above. The contenteditable is actually part of a section of html code. On the serverside with PHP I need to filter out the contenteditable text which I do via a DOMDocument like this:
$doc = new DOMDocument();
$doc->loadHTML($_POST["data"]);
then I access the elements and their textual content as usual.
Finally I save the text with
file_put_contents($txtFile, $plainText, LOCK_EX);
The saved text then was a mess as written above. Now it turns out that you need to tell the DOMDocument the character set wich loadHTML() has to interpretate. In this case UTF-8.
First I did it as recommended in PHP this way :
$doc = new DOMDocument('1.0', 'UTF-8');
But that doesn't help (I wonder). Then I found this answer in SO. And the final solution is this :
$doc->loadHTML('<?xml encoding="UTF-8">' . $_POST["data"]);
Though it works it is a trick. Finally the question is left over, how to do it the right way ? If somebedoy has the definite answer, he is very welcome.
You need to make sure that the content is encoded consistently throughout its roundtrip from user input to server-side storage and back to the browser again.
I would recommend using UTF-8. Check that your HTML document (which includes the contenteditable zone) is UTF-8 encoded, and that the XMLHttpRequest/Ajax request does not specify a different encoding when it sends the content to the server.
Check that your server-side application encodes the text file as UTF-8 also. And check that the HTTP response headers declare the file's encoding as UTF-8 when the file is requested and downloaded in the browser.
Somewhere along this path, the encoding differs, and that is what is causing the error. iconv converts between different encodings, which should not be necessary if everything is consistent.
Good luck!
I have a password protected text file and to make it password protected, i used a password protector script (which works great) but it required me to rename the text file to .php on my server. This went fine, however, when I open this text file in any browser on windows, i do not seeing any new lines (I used to see them)
I tried writing -"\n", "\r", "\r\n". I think it has to do with the browser thinking its a .php file i guess.
This is because the server is sending a different MIME type. It is now sending text/html (the default type returned by PHP) rather than text/plain.
Your browser is then expecting HTML. Line breaks are just like any other white space in HTML, so they are essentially meaningless for what you are trying to do.
You can use this to fix it:
header('Content-Type: text/plain');
Be sure to put that at the top of your code, or at least before you output anything.
This causes the server to send the MIME type you are expecting.
By default the output of PHP scripts are rendered as HTML, which means that whitespace is folded. If you want to change this back to text then you need to set the Content-Type header to "text/plain", either in the web server or via the header() function.
That's because the browser would see the content as html and in html a newline is just a whitespace
I am not sure if I understood your question properly, but in two cases there is a solution:
-You output the text: In this case, you have to use
<br>
-You want to write it with new lines in the file: Use the PHP-Constant
PHP_EOL
which means End-of-Line. This inserts always a correct break.
You need to use <br>, as html is being rendered in the browser.
Browsers uses HTML to format text (contained in html of course)
use <br> or <p>
Also considering your file is a .php it's a normal behaviour that your webserver will send it as text/html
My friend has been playing around with some language stuff on our site and our file names are being out put with these characters now. Usually I'd wait for him to wake up but this is a pretty big issue as we are getting e-mails through about the weird characters in the file names.
You don't see the characters when echoed in HTML, but we have the names being output to a header, which does show the characters, like so:
header('Content-Disposition: attachment; filename="'.$title.'.'.strtolower($type).'";');
How can we avoid these characters from displaying? They are also being input to our database, file names such as asdfmovie - I have googled the codes but I can't find any results for them.
Does anyone know what they are? and how to avoid them?
Thank you
html_entity_decode()
http://php.net/manual/en/function.html-entity-decode.php
These are html entities that are valid in HTML. Your email client is actually encoding them into HTML entities (a double effect), which means that the actual entities are what you're seeing. Just make sure that anything passed into the email runs through the html_entity_decode() function.
These are HTML entities which can be decoded using html_entity_decode, like echo html_entity_decode($str, ENT_COMPAT, 'UTF-8').
It's wrong to store such values in the database though, as you are seeing. The values should be stored in their original form and only HTML entity encoded when necessary for outputting to HTML. Figure out where they're being HTML encoded and fix that. If you already have a database full of this nonsense... um, have fun reversing it. :o)
i'm working on a website. I have a file in php and I want to include it. The file contains a big array, nothing else.
When I tried to include it, the page functioned normally but I needed to convert my included file from ANSI to UTF8. After converting (notepad++) i get a big space at the top of the screen. Using firebug I was able to figure out that when I include the UTF8 encoded version of the file, it echos break lines... As you would assume, that is the kind of behaviour i did not expect. How to convert the file so that it does not echo itself for no reason?
Try to convert it to UTF-8 without BOM.
Make sure your file looks like this (there is nothing before <?php):
<?php
$array=array(
/* elements */
);
put the
<?php
open tag at the very beginning of the file.
And do not include a ?> tag at the end of your file, if it only contains PHP code.
i have a txt file with a list of country's. For my form i just read all the data in a select list line per line with fgets(). And that works fine except for some problems.
1) When i have a country with ¨ on a letter it comes in the list just as a blank.
2) When i put the data in an xml at the end it seams there is a return at the end of each value in the form of '
'.
so my question. Is there either a way to fix these problems or is there a better way to read data from a file. Or should i use on other filetype then txt?
It sounds like a trouble with the text encoding. You could try to run htmlentities on the text before echo:ing it out. Another solution is to use utf8_encode or utf8_decode (depending on which encoding your pages are served as, and on the encoding of the file).
In character data, the carriage-return (#xD) character is represented by
Just make sure that after you've read each line, you str_replace('\r', '', $line) each line to remove the carriage-return character from the end of the line.