Storing results of script execution to a file - php

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.

Related

Printing file output to printer?

I am trying to generate some contents dynamically from a PHP page for print. Currently, I am using a file to store contents and then send that file to the printer. Is there a way, I can store contents in a variable or buffer and send to printer directly with out saving them in the file.
My current code:
ob_start();
include 'printcheck.php';
$result = ob_get_clean();
file_put_contents('/var/www/prints/rt/rtorder.txt', ob_get_contents());
// end buffering and displaying page
ob_end_flush();
Print it.
exec("cat "/var/www/prints/rt/rtorder.txt" | lp -d "TSP100");

Prevent downloading media unless the user has filled in a form

Im making a website for a band, and they are giving out their EP for free, but they want it so the user has to enter their email address before downloading... How would this be done in php?
The downloadable should be placed out of the reach of web user but within your PHP script reach. Then once user is done filling form, you can then force download the file contents by opening it locally using say "fopen".
Update (Adding Sample Code):
Suppose the file is "txt.txt" which could be in your script reach. You will open it, read and then put the contents after calling header and telling it that its an attachment (force download)
$done = true;
if($done == true){
$filename = "txt.txt";
$conn = fopen($filename,"r");
$contents = fread($conn, filesize($filename));
fclose($conn);
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="downloaded.txt"');
echo $contents;
}
If you're storing the emails somehow, then simply set a $_SESSION value when they submit their email, and when writing the page, if the part of the $_SESSION value has been set, then provide a link to the media.
To start a session & set the value:
session_start();
$_SERVER['hasEmail']=true;
And in the page:
session_start();
if ($_SERVER['hasEmail']) {
//Provide link
}
You could then also have the media link take you to a PHP script which uses fopen() or similar to get the file from another location on your filesystem out of reach of the user, such as one under .htaccess blocking, and it'll only provide the media is the $_SESSION value is set.

How to save an XML file to local file system (client) in PHP?

In my site, I have implemented the following functionality: If the user clicks on a button it triggers a PHP function which generates an XML file (that PHP function is called by AJAX). Everything is working well, but here's one thing I want to change: I don't want an .XML file to be created on the server machine; instead, I want the user to be prompted to save the .XML file locally. How do I do that? My PHP script currently looks like this:
$xml = new DOMDocument("1.0", "UTF-8");
$rootElement = $xml->appendChild($xml->createElement("SomeNodeName"));
...
// the following doesn't really work - xml doesn't get formatted :)
$xml->formatOutput = true;
// this creates the actual file and places it on server. that's not what i need
$xml->save("MyXMLfile.xml");
Thanks for all the help.
Every web content has a header, so if you specify the header for an xml file (through the use of the header() function with the appropriate code it'll work.
This would mean doing something like this:
<?php
header('Content-type: text/xml');
// set the filename
header('Content-Disposition: attachment; filename="pwet.xml"');
// echo the content here
echo $xml; // simply like this, maybe?

How to save a search Query (Copying the file and Saving )

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);

Save output of a php file in a html file

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

Categories