Make and display plaintext file
Posted: Wed Jun 29, 2011 7:02 pm
I'm trying to run a php file to save some data to a plaintext file and have that file displayed in the browser.
I can save the data to the file (like this), but I don't know how to display the file in the browser immediately.
$outfile = "/tmp/output.txt";
$fp = fopen ($outfile, "w");
$lines = file($infile);
foreach($lines as $line_num => $line)
{
echo fputs ($fp, $line);
}
fclose($fp);
Now I can put in a link <a href="..."></a> to the file and click on it, but I'd rather achieve the result without clicking (and besides, the file I've just put the data in isn't addressable via such a link).
Alternatively, if I put the data straight into a page like this
<?php
$infile = "input.txt";
$lines = file($infile);
foreach($lines as $line_num => $line)
{
echo $line;
}
?>
(That's the entire php file.)
then the data comes up in the browser fine, but it's not formatted like plain text (not Courier New, no line feeds, etc.).
Adding formatting is not an answer, as the file must contain only plaintext when downloaded.
I'm obviously missing something. Can anyone tell me what it is!
Thanks.
I can save the data to the file (like this), but I don't know how to display the file in the browser immediately.
$outfile = "/tmp/output.txt";
$fp = fopen ($outfile, "w");
$lines = file($infile);
foreach($lines as $line_num => $line)
{
echo fputs ($fp, $line);
}
fclose($fp);
Now I can put in a link <a href="..."></a> to the file and click on it, but I'd rather achieve the result without clicking (and besides, the file I've just put the data in isn't addressable via such a link).
Alternatively, if I put the data straight into a page like this
<?php
$infile = "input.txt";
$lines = file($infile);
foreach($lines as $line_num => $line)
{
echo $line;
}
?>
(That's the entire php file.)
then the data comes up in the browser fine, but it's not formatted like plain text (not Courier New, no line feeds, etc.).
Adding formatting is not an answer, as the file must contain only plaintext when downloaded.
I'm obviously missing something. Can anyone tell me what it is!
Thanks.