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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.