Printing php script output to file
The easiest method would be to create a string of your HTML data and use the file_put_contents()
function.
$htmlStr = '<div>Foobar</div>';
file_put_contents($fileName, $htmlStr);
To create this string, you'll want to capture all outputted data. For that you'll need to use the ob_start
and ob_end_clean
output control functions:
// Turn on output buffering
ob_start();
echo "<div>";
echo "Foobar";
echo "</div>";
// Return the contents of the output buffer
$htmlStr = ob_get_contents();
// Clean (erase) the output buffer and turn off output buffering
ob_end_clean();
// Write final string to file
file_put_contents($fileName, $htmlStr);
Reference -
ob_start()
ob_get_contents()
ob_end_clean()
file_put_contents()
PHP output control documentation
How to redirect echo command output to a text file in PHP
Just use fopen/fwrite
to write to a file (this will create the output.txt if it does not exist, else overwrites it)
$myfile = fopen("output.txt", "w") or die("Unable to open file!");
fwrite($myfile, $TOKEN);
fclose($myfile);
Include a php file and write the output to a file?
Use output buffering to capture the data and file_put_contents()
to save the captured output.
ob_start();
include 'content.php';
$content = ob_get_clean();
file_put_contents('file.txt', $content);
Store PHP Output To File?
You may want to redirect your output in crontab
:
php /path/to/php/file.php >> log.txt
Or use PHP with, for example, file_put_contents()
:
file_put_contents('log.txt', 'some data', FILE_APPEND);
If you want to capture all PHP output, then use ob_
function, like:
ob_start();
/*
We're doing stuff..
stuff
...
and again
*/
$content = ob_get_contents();
ob_end_clean(); //here, output is cleaned. You may want to flush it with ob_end_flush()
file_put_contents('log.txt', $content, FILE_APPEND);
Saving php output in a file
Try this out using the true
param in the print_r
:
$f = fopen("file.txt", "w");
fwrite($f, print_r($array2, true));
fwrite($f, print_r($array3, true));
fwrite($f, print_r($array4, true));
fclose($f);
PHP Print to txt file and Redirect after form submit
You need a newline after data. Then use the PHP header() function to redirect.
You could use "\r\n"
as the newline but I like PHP_EOL
:
<?php
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite($fp, $data . PHP_EOL);
fclose($fp);
header("Location: http://www.yoursitehere.com");
}
?>
Printing / Echoing To Console from PHP Script
Thats the method of doing it.
Things to check for are output buffering
http://php.net/manual/en/function.ob-flush.php
Is that code actually being run ? Make sure it doesnt branch before it gets there
Related Topics
JSON_Encode Won't Encode French Characters
How to Read the Query String in PHP and HTML
Php/Apache: PHP Fatal Error: Call to Undefined Function MySQL_Connect()
Elegant Way to Search an PHP Array Using a User-Defined Function
How to Decode a Base64 String (Gif) into Image in PHP/Html
How to Solve This "Warning: MySQLi_Connect(): (Hy000/1049): Unknown Database" Problem
Differencebetween Crc32 and Crc32B
Php: How to Load the Content of a Web Page into a Variable
Save Curl Display Output String in Variable PHP
Laravel 5 Multiple Download File
Clear Data in MySQL Table with PHP
Alternative to MySQLi_Fetch_All Needed
How to Use PHP Inside CSS File
Does PHP Feature Short Hand Syntax for Objects
Get Last Word from Url After a Slash in PHP
Merge a Table and a Change Log into a View in Postgresql