Jump to content

fwrite or file_get_contents help


smidgen11

Recommended Posts

Hi,

 

I am trying write the contents of a php file's output, which echos out a small output of text based on variables and calulations, to a single file. I have tried  fwrite and file_get_contents. When I run my code I get the file to create but the file it creates only outputs "1". Here is a look at my code:

 

fwrite version:

 

$text= include '$Site-2851.php';

$file = fopen("myfile.txt","w");
echo fwrite($file, $text);
fclose($file);

 

file_get_contents version:

 

$text= include '$Site-2851.php';
$file = 'myfile.cfg';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $text;
// Write the contents back to the file
file_put_contents($file, $current);

 

Any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/252510-fwrite-or-file_get_contents-help/
Share on other sites

include() returns true or false depending on whether the include was a success or not.  If you file contains a 'return' statement it will return that instead.  If you file just contains a bunch of echo's though and you want to capture that, you need to use output buffering.

 

ob_start();
include 'file.php';
$output = ob_get_clean();

//write $output to file

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.