I want to copy the file http://searchr.us/Testing/web-search.phtml?search=SEARCHED+TEXT to
http://searchr.us/Testing/search/SEARCHED+TEXT.html
How do i do this?
NOTE:The source of http://searchr.us/Testing/search/SEARCHED+TEXT.html should be the same as http://searchr.us/Testing/web-search.phtml?search=SEARCHED+TEXT
Indirectly I'm just saving a query so that I can keep a track of them!
Hope i've get you right: you want to store output of any query into file. You can manage this with output buffering.
In the begining of script wright:
ob_start(); // start buffering
This turns buffering on.
At the eng of page wright something like this ($query is text of query):
$html = ob_get_contents();
ob_end_flush(); // this turns off buffering and sends buffered content to output
$fp = fopen("{$query}.html","w");
fwrite($fp,$html);
fclose($fp);
Related
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.
Is there any command to get the output and erase the buffer without turning it off?
eg.
ob_start();
include 'first_page.php';
$first_page = ob_get_clean();
ob_start();
include 'second_page.php';
$second_page = ob_get_clean();
ob_start();
....
Is there a function available so I don't have to turn on output buffering every time?
I know this is an old question but I was looking for an answer to this question and wanted to post the answer based on comments posted here.
It looks like the answer is no, there is no single command to get the output buffer and erase it without turning it off. However, instead of repeatedly starting a new buffer, you could just erase the current buffer with ob_clean() in between calls of ob_get_contents()
For example:
ob_start();
include 'first_page.php';
$first_page = ob_get_contents();
ob_clean();
include 'second_page.php';
$second_page = ob_get_contents();
ob_end_clean();
I'm having a hard time figuring out the problem in the following code, I really need a solution to this.
Consider the following code :
<?php
//starting a new output buffer, with a GZIP compression
ob_start("ob_gzhandler");
//this goes into the buffer
echo "Hi";
//grabbing the buffer's content
$content = ob_get_contents();
//cleaning the buffer
ob_clean();
//we're still inside the buffer, show the content again
echo $content;
This code fails to output "Hi", instead I see "‹óÈM", what have done that broke correct buffering? Knowing that once I remove "ob_gzhandler", the buffering is correct and everything is fine. I don't want to create another buffer and destroy the current one. I just want to clean the current one using ob_clean.
Any ideas? Thanks in advance.
Thank you for your answer, I figured out why, GZIP is insalled on my machine by the way, it's that when setting ob_gzhandler, the buffer is compressed chunk by chunk, so when using ob_get_contents(), parts of the last chunck are missing, and I end up getting weird output.
To correct that behaviour (or at least to bypass it), open a second output buffer, and leave the one with gzhandler() alone.
Like this
ob_start("ob_gzhandler");
ob_start();
Now the second one isn't compressed, I can do whatever I want with it (hence get its content, clean it etc). The content will be compressed anyway given that a higher level output buffer with gzhandler is opened.
Maybe you don't have gzip compression enabled/installed on your machine.
Tried your code and got something like that. I don't have gzip installed on my machine, try this:
It's your code but with a condition, if gzip doesn't start, the buffer starts.
//starting a new output buffer, with a GZIP compression
if (!ob_start("ob_gzhandler")) ob_start();
//this goes into the buffer
echo "Hi";
//grabbing the buffer's content
$content = ob_get_contents();
//cleaning the buffer
ob_clean();
//we're still inside the buffer, show the content again
echo "<pre>"; echo $content; echo "</pre>";
ob_end_flush();
If you get "Hi", maybe gzip is not installed.
I have built an estimating application where I type in some fields hit submit and the data is saved to my database and creates an on the fly estimate with everything that I typed in. Now they issue I am having is I want to save the output page as a file to my webserver for later viewing, but I can only find examples that save all of my code including the ECHO $Field1. How can I save the outputted results to a file. The same results one would see if they right clicked on the outputted page and choose view source. I was hoping to do it only using PHP.
I tried this already.....
<?php
ob_start();
// all your logic and code for displaying
$output = ob_get_contents();
file_put_contents($uniquehtml,$output);
// save output page to the html file
?>
use something like this in your output page
//buffer output
ob_start();
//process form
//your code goes here
//save & flush buffer in a file
$buffer = ob_get_flush();
file_put_contents('buffer.txt', $buffer, FILE_APPEND);
The FILE_APPEND keeps adding output to the same file. Otherwise, create a new file name each time it writes. You could append time() to the file name, for instance.
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