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
Related
When we say that a function's result is sent to output buffer, it means that the result is not visible until echoed. And for functions that output directly, we use ob_start to direct the result to the output buffer (before it hits the browser as plain html) so that we may manipulate it, and then if desired, echo it.
The result of the function readfile() is directly visible, which is the contents of a certain text file, for example. then my question is:
Why it is mentioned in php documentation that readfile() sends the contents to the output buffer ?! (while in fact it is displayed directly).
Am I missing something?
When we say that a function's result is sent to output buffer, it means that the result is not visible until echoed. No it doesn't mean that!
Because it is sent to the output buffer, which (unless you've explicitly executed an ob_start() to buffer the output before sending it to the browser) is sent directly to the browser (or whatever).
All output is sent to the output buffer, but unless you've set the buffer to hold output before sending it, it gets sent.
EDIT
Of course, a webserver may be configured to cache output until a certain volume of data has been sent, but that isn't the same as PHP output buffering
Well, when the readfile() is executed, the output goes directly to stdout.
it is useful to know this behavior when php is used as a bash script:
#!/usr/bin/php
<?php
readfile("cool.txt");
// Output cool.txt whole content
It behave differently to file_get_contents from this view.
#!/usr/bin/php
<?php
file_get_contents("js.txt");
// No output!
$cool = file_get_contents("js.txt");
echo $cool;
// Output cool.txt
I'm using PHPUnit to test a function which downloads a file. I want to test that the correct file is downloaded and so my idea was to check the output of the function. I'm trying to use output buffering:
ob_start();
$viewer->downloadById($fileId);
$output = ob_get_flush();
$this->assertEquals($expectedFileContents,$output);
The test passes/fails when it should, which is good. My issue is that the contents of the output buffer is also printed to the console. How do I hide this?
Use ob_get_clean() instead of ob_get_flush(). The former will remove the buffer without printing it and return its contents. The latter will do the same and print the contents of the buffer.
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.
If I generate an image directly in a php script to be sent back to JS, is it mandatory to have this interoom step of saving the image to the file system ?
I create an image using imagecreate() then imagecopyresized() then want to send the result as base64 wrapped in json (Later I will want an array of strings rather than just one).
I couldn't find anything that base64_encode() would take in apart from the bytes from fread(). I find having to save to the file system wasteful and think there must be a better way!
Thanks
You don't have to save the file at all. Simply use output buffering:
ob_start();
imagepng($im);
$pngData = ob_get_contents();
ob_end_clean();
echo base64_encode($pngData);
Just want to add; There's a function ob_get_clean() which executes both ob_get_contents() and ob_end_clean()
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.