How to stop PHP prefixing LF to start of file - php

I am using PHP to pass some information in a text file back to the user which is then used as input for an app. I'm using the method shown in the following snippet to serve the file to the user.
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=filename.dat');
echo $data;
exit();
I get the save as dialogue fine and the data is present the only problem is that there is a line feed character at the start of the output file which I cant seem to get rid of.
It just so happens that the app that uses the data is sensitive to white space and although it would be easy to fix this, forcing the users to update is not a route I want to go down for various reasons.
Does anyone know what is causing this and more importantly any workarounds.

As I already said in the comments to the question:
Either you $data contains that line feed or there is something before that snippet that does this. Maybe a line feed before you opened the PHP block.

Probably $data contains the line feed.
Look for includes too

Presumably the extra newline is getting into $data somehow. If you can’t fix that, you could trim() the data before you echo it.
Can you post how you're setting $data?

Just an additional note.
In case you are working on a project where you cannot identify the files that have the leading or trailing line feeds, new lines, carriage returns just call the code below before your headers:
//Loop through any open buffers and nuke them.
while(#ob_end_clean());
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=filename.dat');
echo $data;
exit();
This may have some unexpected side-effects on the code that relies on those buffers but is an effective way to completely clear out your output buffer.

Maybe you can use ob_get_contents or ob_get_length to see if anything has been sent to the output before the echo statement. Or use ob_clean before the echo.

Related

PHP - Download file(s) from the server without revealing the URL - A few GOTCHAS

I am working on a "simple" shopping cart app that allows users to purchase downloadable products. I am interested in creating a link to a digital product without revealing the full URL to the end user. I have searched other solutions here and understand the basics of making a "download.php" file that references the product ID, stored as a HASH type value in the DB, that can then be referenced to get the actual URL of the file.
However, there are some formatting issues that I've bumped into that others might find interesting:
The following link is an example: Download files from server php
It shows the following code as the headers of the downloadable document (Referencing a PHP variable):
header("Content-Disposition: attachment; filename=$file");
Another example of that is here: Start a download with PHP without revealing file URL
readfile($file);
However, if you look at the PHP HEADERS documentation here: PHP - HEADERS Documentation
You'll notice that all of the arguments passed are wrapped in single quotes:
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
Those who are particularly gifted in attention to detail will also notice that the Content-Disposition argument has the filename wrapped in double quotes . . . with the full argument wrapped in single quotes.
If I followed the code in the above examples (leaving out the single and/or double quotes) the file seems to be corrupted, missing, or just fails to load. So how do I fix this?
To get this working in my own code, I needed to wrap my PHP variables in either single or double quotes (and sometimes doubled single quotes) as shown below:
header('Content-Type:'.$ctype.'' );
header('Content-Disposition: attachment; filename="'.$fname.'"');
readfile(''.$filepath.'');
When entered this way, I was able to download the files without corruption or issue. Please let me know if you are aware of a better way of doing this.
However, when I used the coded above it also allowed me to NOT use the
ob_clean();
ob_end_flush();
commands in my code. In fact if I add them now . . . it seems to corrupt my files too. Weird.
Another potential GOTCHA is that if you attempt to download a TXT file, and you have a PHP "echo" statement anywhere in your "download.php" file, the contents of the ECHO statement will be appended to the TXTY file that's downloaded. So just something to be careful of.
This ECHO statement may also be appended to the headers of other documents types as well, but it didn't seem to affect the ability of the file to open in my limited testing. But again, be careful out there! :)

Downloading file from server PHP - File is slightly off

I hope this is a simple and quick fix. I have looked on here already to learn how to force download a file from the server. Here is what I am using below:
<?php
// Sending the file - a pdf in this case
header('Content-type: application/octet-stream');
// Specify what the file will be called
header('Content-Disposition: attachment; filename="1234.txt"');
// And specify where it is coming from
readfile('C:\test\1234.txt');
?>
It should be pretty self explanatory but I have a text file saved at C:\test\1234.txt.
I link to my php file (that has the above code) which is called download.php using this from the HTML page:
Download This File!
OK now the problem.. My original text file is this:
test
but when I download the file, the result is a carriage return above:
_
test
The problem isn't huge with the text file, but because of this issue, all other files downloaded are corrupt and I believe this is why. I am hoping that someone has a solution to this that is pretty simple.
Thanks in advance!
You might try removing the PHP closing tag. Note this quote from php.net
If a file is pure PHP code, it is preferable to omit the PHP closing
tag at the end of the file. This prevents accidental whitespace or new
lines being added after the PHP closing tag, which may cause unwanted
effects because PHP will start output buffering when there is no
intention from the programmer to send any output at that point in the
script.
Perhaps that's where you're getting the extra carriage return.

DOMPDF: Unable to stream pdf: headers already sent

I am generating php page using smarty template based on html form input and uploaded files. Using dompdf, I want to save the generated page as a pdf file.
When the user submits the multipart/form-data, data is posted to itself. Then it undergoes validation process. When all is fine, a new page is generated using a template file. There is no output, instead, dompdf utilizes the template file to stream the pdf file. After solving several stages of problems such as "DOMPDF not found", insufficient memory etc, I am now stuck with "Unable to stream pdf: headers already sent" error.
One of the most common problems is presence of line break, white space or any output being before stream() is called. I checked for white space before and after <?php and >?.
There are nor print_f or echo statements either. How can I troubleshoot this problem? Where does the problem lie...in the smarty template file or the php file itself?
Here is the code:
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$html = $smarty->fetch('index.tpl');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
$dompdf->stream("newfile.pdf");
simple solution :
write below lines before stream, it will show where exactly new line or space is coming
$f;
$l;
if(headers_sent($f,$l))
{
echo $f,'<br/>',$l,'<br/>';
die('now detect line');
}
I had the same problem, and I solved this problem by adding this code in the top file:
ob_start();
Most probably there's a whitespace or new line somewhere in your code causing this. Here's a simple way to debug it:
echo something at the very end of your script (above the stream() call), for example echo "end!";exit;
Click to "view source" of your page, which makes spaces easier to see
If your "end!" string does not appear at the very start of your script, then somewhere there's a character printed
Move the "echo end!" line further up to your code, until you locate where the space was inserted
Another possibility is that you are using a language string somewhere that introduces an unprintable character. If your application is multilingual, make sure you're testing using english
Replace line 3105 of this file: dompdf/lib/class.pdf.php
if ( headers_sent()) {
die("Unable to stream pdf: headers already sent");
}
With
$output = ob_get_clean();
if ( headers_sent()) {
echo $output; }
Then in the file that is generating the pdf output e.g if you were outputting from a Joomla Component
components/com_joomlacomponent/views/yourpdfdir/tmpl/default.php
Enter immediately after the opening php tag
<?php
ob_start();
I tried to echo out, but no white spaces or line breaks were found. At the end, I redirected the php to another page instead of PHP_SELF and the problem vanished. I did not alter any code. Looks like presence of html tags after the php ended was the offending factor.
Be sure your editor does not add a Unicode Bom description - save code to file by Notepad, or (if you work in Dreamweaver) remove check by Asign Unicode Signature (BOM) or something. ;)
I came across this issue. You want to check all the variables you are using.
One or even more than one variable you are passing comes empty and is messing the render.
Start gradually, by getting rid of all the php and try to generate the pdf, then if it works for you add code block by block.
This will help you identify where the problem is.
I had this issue, with no apparent output when viewing the source. The problem for me was that I had flushed the output, even though there was none except the headers, and that blocked streaming the output giving the "headers already sent" message. Which was true. My fix was to remove the flush() and ob_flush() statements, and the streaming output then worked.
for me - solution was to encode my file to UTF-8 instead of UTF-8 BOM
In my case the problem was solved by setting $_dompdf_show_warnings to false in dompdf_config_inc.php

Broken image with readfile and file_get_contents

I'm trying to use a function in order to get this working:
<img src='login.php?image=ind_legend.jpg'>
But I can't pass through the function to place the image. I went back a couple of steps and tried only this part of the code:
<?php
$file = "http://localhost/sales/test.jpg";
header('Content-type: image/jpeg');
readfile($file);
?>
or using this function:
echo file_get_contents($source);
but the fact is that the only thing I get is a broken image cross (IE) or nothing in Firefox.
I would appreciate any suggestions
Thanks in advance
use the ob_clean() function of php before the readfile()
You certainly have some whitespace in your PHP script, or a UTF-8 BOM invisibly before your <?php opening marker. Use a hexeditor to find out.
To debug it further, open the image URL http://localhost/login.php?image=ind_legend.jpg directly in your browser, save the file. And then compare it to the original JPEG.
As previously mentioned, you probably have some whitespace. I'd try replacing the entire file with the code below. Removing the closing php statement eliminates any chance that there is extra whitespace:
<?php
$file = "http://localhost/sales/test.jpg";
header('Content-type: image/jpeg');
readfile($file);
first of all point your browser to http://youraddress/login.php?image=ind_legend.jpg and check the result.
Maybe the file /sales/test.jpg is corrupted or you don't have enabled the http:// wrapper for readline
At last save the corrupted image via the save image as... context menu option of your browser of choice and try to open it with a text editor. I will not surprised if you will find an error message (if you have them enabled).

content type adds extra html

for the download forced, when I save the file it has some extra html test.
my code
<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="abc.txt"');
echo "test";
?>
when I save this is what I have
<script language="javascript">
// some garbage
//-->
</script>
test
I want only test.
Don't include the include file.
At the risk of stating the obvious, something is already outputting some code/including a file, etc. before you're trying to set the headers.
Whilst the cause of this is likely to be specific to your web app (you'll need to check precisely what's being output from the ground up), the requirement is that the headers need to be the very first (before any HTML, etc.) things output.
Based on your comment, do you mean that the JavaScript being outputted is in the PHP file that's doing the outputting, and you want it to not output that?
The first thought would be to remove the JavaScript. But beyond that, you may find some use from something like ob_start() to capture your output buffer and manipulate it before sending it to the client.

Categories