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.
Related
I'm trying to save the content of a file into a PDF using html2pdf, but the file has some PHP codes which need to be processed. I made some research and I found out that I had to use output buffering so that the PHP content in the file can be processed. So I did something like:
<?php
require_once('html2pdf.class.php');
ob_start();
require_once('my_file.php');
$content = ob_get_clean();
// force download of $content to a PDF
$html2pdf = new HTML2PDF('P','A3','fr', false, 'ISO-8859-1');
$html2pdf->writeHTML($content);
$html2pdf->Output('file_name.pdf', 'D');
?>
The file my_file.php is the file that has some PHP code and the HTML content that I wanna save to a PDF, and the variable $content is the actual content with the PHP codes processed and everything. This works fine on Apache, but not on IIS.
Does anybody know an alternative way to make this work witout using ouput buffering? I tried file_get_contents('my_file.php'); but my php contents in my_file.php do not get processed when I do so.
Please, I'm looking for ways to do this without output buffering so that it can work on any server. I'm not looking for answers telling me to change my IIS server configuration or to use something else other than html2pdf.
Thanks in advance for any help
If you can modify the contents of my_file.php, you can put all the text into a variable there instead of outputting it directly.
You can use PHP/PDF Library http://php.net/manual/en/book.pdf.php
And follow this example : http://php.net/manual/en/pdf.examples-basic.php
Hope that helps :)
The easiest approach would be to edit my_file.php so that rather than containing HTML it assigns the HTML content to a PHP variable. Then all you need to do is echo the variable.
//other PHP processing goes here, or anywhere else.
$someVar = "hello world";
$myHTML = "<html>My output: $someVar </html>";
echo $myHTML;
It's an ugly way of handling HTML output, and I'm not saying it's good programming, but if you want to avoid editing config files it would be quick and easy.
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.
I have a very basic question illustrated by the code snippet below.
This is the relevant part of a much bigger program in which I wish to download a zip file. The code as shown, with the comments in place, produce the expected browser output "Download the file now."
When I un-comment the code, the zip file is correctly downloaded to my browser!
However, the browser output is not produced. How do I regain control? I would like for the user to then have other options. (By the way, un-commenting the single "Content-disposition" line is sufficient to cause the loss of control.)
I have tried including the code, putting it in a function, many possible combinations of ob_start, ob_end flush, etc., all to no avail. I am sure I am overlooking something very fundamental and would appreciate some suggestions.
Thanks.
<?php
$sZipFN = 'file.zip';
// header("Content-type: application/zip");
// header("Content-disposition: attachment; filename=$sZipFN");
// readfile($sZipFN);
$sMsg = "Download the file now.";
?>
<html>
<body>
<p> <?php echo $sMsg; ?> </p>
</body>
</html>
You can't - you have to redirect to the options page and then start the download via javascript redirect (document.location = 'http://download.url/';)
This will start the download and leave the user on the options page like you desire.
Don't forget to include a 'Click here if the download fails to start' link somewhere near the top of the page (just in case javascript is disabled)
I am having a php file which executes some code to generate a html file.Its like I m having a form from which some data will be posted to x.php file, which gives a output(a webpage). I want to save that output in a html file.What the most efficient way of doing this.?
EDIT
I want to save it on sever side. Actually the thing is i want to create pdf file for that.. I wrote everything else.Now the thing is i want to save the output in a html page.So that i can convert it into pdf file..
Try something like this:
// Start output buffering
ob_start();
// run code in x.php file
// ...
// saving captured output to file
file_put_contents('filename.htm', ob_get_contents());
// end buffering and displaying page
ob_end_flush();
If you cannot use the ob_* functions, you can also write the form to a variable and then save that variable.
Look at ob functions (see http://php.net/manual/en/function.ob-start.php) that allows you to capture every output (echo, print, etc...) from the page.
using ob_* functions such as ob_get_contents(), a php script can catch it's output.
probably with ob_start and output_callback see http://php.net/manual/en/function.ob-start.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.