0

I'm trying to write to a file in PHP to cache the output of a small portion of code.

        ob_start();
        echo "Hello";
     $fp = fopen("cache/ttcache.php", 'w');
        fwrite($fp, ob_get_contents());
        fclose($fp);
        ob_end_flush();

The file exists and is blank. The fwrite function points to the correct location. It just doesn't write.

Any help?

3
  • What happens if you simply echo ob_get_contents()? Commented Apr 14, 2011 at 5:47
  • Can you show some minimal code that reproduces the problem? Something that actually produces output, i.e. where the // my code that creates an output is filled in. Commented Apr 14, 2011 at 5:48
  • ob_get_contents() holds the correct data, I just checked it out. Commented Apr 14, 2011 at 5:49

2 Answers 2

2

Try to write first small word or sentence first.

fwrite($fp, 'hello')

Also check your file permission's should be writable.

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't write a word, I think you're correct in that this is a permissions problem. I'll make it writable if it isn't.
0

Make sure ob_get_contents() is really not empty. Then try

fflush($fp);

right before fclose().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.