943,925 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2598
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 20th, 2008
0

fwrite not working with ob_start, Any ideas?

Expand Post »
Hi,

Thanks for checking this out.

The error is that when I use ob_start to store the results of an include file my fwrite statement later on in the code works but only writes a blank file, whereas when the ob_start is not present the fwrite function writes the file with all the content present in it.

Anyone out there smarter than me about this stuff (not hard) please let me know how to solve this. Thanks.

php Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4.  
  5. //Storing the code to load into each div. Held in other php files to aid code resue
  6. ob_start(); // start buffer
  7. include ('../php_includes/sidebar_search_package.php');
  8. $package = ob_get_contents(); // assign buffer contents to variable
  9. ob_end_clean(); // end buffer and remove buffer contents
  10.  
  11. ob_start(); // start buffer
  12. include ('../php_includes/sidebar_search_flight.php');
  13. $flight = ob_get_contents(); // assign buffer contents to variable
  14. ob_end_clean(); // end buffer and remove buffer contents
  15.  
  16. ob_start(); // start buffer
  17. include ('../php_includes/sidebar_search_hotel.php');
  18. $hotel = ob_get_contents(); // assign buffer contents to variable
  19. ob_end_clean(); // end buffer and remove buffer contents
  20.  
  21.  
  22.  
  23.  
  24. $filename = "../js_includes/SearchDiv.js";
  25. $file = fopen($filename,"w");
  26.  
  27. if(!$file){
  28. echo "Error creating searchDiv2";
  29. exit;
  30. }
  31.  
  32. if(!is_writable($filename)){
  33. exit("$filename is not writable");
  34. }
  35.  
  36.  
  37. //Variable to hold content to be written to the javascript file. Double quotes are used for surrounding content and any quotes within the JS file should use single quotes or escaped double quotes
  38. $to_write = "
  39.  
  40. function searchSwitch(DIV) {
  41. switch (DIV)
  42. {
  43. case 'package':
  44. package = '<p>Package loaded</p>';
  45. document.getElementById('package').innerHTML = 'test';
  46. document.getElementById('hotel').innerHTML = '';
  47. document.getElementById('flight').innerHTML = '';
  48. break;
  49. case 'hotel':
  50. hotel = '<p>Hotel loaded</p>';
  51. document.getElementById('package').innerHTML = '';
  52. document.getElementById('hotel').innerHTML = 'test';
  53. document.getElementById('flight').innerHTML = '';
  54. break;
  55. case 'flight':
  56. flight = '<p>Flight loaded</p>';
  57. document.getElementById('package').innerHTML = '';
  58. document.getElementById('hotel').innerHTML = '';
  59. document.getElementById('flight').innerHTML = '<p>Flight Loaded</p>';
  60. break;
  61. default : alert('No search cat selected');
  62. }
  63. }//Closing function opening
  64.  
  65. ";
  66.  
  67. $to_write = '<p>Test</p>';
  68.  
  69. $write = fwrite($file, $to_write);
  70.  
  71. if( !$write ){
  72. echo "Content could not be written to the file $filename";
  73. exit;
  74. }
  75.  
  76. ?>
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Nov 20th, 2008
0

Re: fwrite not working with ob_start, Any ideas?

You should use fopen with parameter 'a' or 'a+'instead of 'w' in this way if file exists new data will be appended, otherwise php will attemt to create it.

http://bg2.php.net/manual/en/function.fopen.php
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 20th, 2008
0

Re: fwrite not working with ob_start, Any ideas?

Ooops...my bad. I thought you want to append content rather than replace it.

In this case 'w' is correct option for fopen to be used. Maybe w+, but in any way, as I have read your code more deeply, the fopen function may not be the issue...

However, if I am not mistaking, ob_end_clean clears the buffer without output, while ob_get_flush output the buffer as a string. The difference seems to be that end clean simply empties the buffer with no output, while get_flush outputs a string and closes buffering.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 20th, 2008
0

Re: fwrite not working with ob_start, Any ideas?

i really cannot see how ob_start() would have anything to do with not being able to write to a file. are there any php errors you are getting?

also, you are overwriting $to_write at the end of your script, so its not allowing the js to be written to the file. are you doing that on purpose?
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 21st, 2008
0

Re: fwrite not working with ob_start, Any ideas?

Hi Rhyan and kkeith29.

Thanks for the replies. kkeith yes I am overwriting $to_write just as a temporary troubleshooting aid.

Rhyan I tried changing the code to this (The old code is commented out to let you know whats changed):

php Syntax (Toggle Plain Text)
  1. ob_start(); // start buffer
  2. include ('../php_includes/sidebar_search_flight.php');
  3. //$flight = ob_get_contents(); // assign buffer contents to variable
  4. //ob_end_clean(); // end buffer and remove buffer contents
  5. $flight = ob_get_flush();

However this doesn't work as the included file is output as content and displayed in the browser window and th ejavascript file is not created.

I'm going to try and put the content held int he include file directly in the code and escape every quote I need to and I'll see if that works, but if you have any other ideas please let me know as i really want to keep the content separated and not have to escape everything.

Many thanks,

Richard

EDIT; there are no PHP errors being output either. using the first code is is just a blank page (which is good as I will be using it with a CRON job) or with the modified code the include file is output.
Last edited by rickya100; Nov 21st, 2008 at 5:47 am. Reason: Forgot some information
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Nov 21st, 2008
0

Re: fwrite not working with ob_start, Any ideas?

i rewrote the code. see if it works. sometimes you miss the smallest things and rewriting it all over usually solves the problem.

i also included a function because repetitive code drives me crazy.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. function getFile( $file ) {
  4. ob_start();
  5. include( $file );
  6. $data = ob_get_contents();
  7. ob_end_clean();
  8. return $data;
  9. }
  10.  
  11. $package = getFile( '../php_includes/sidebar_search_package.php' );
  12. $flight = getFile( '../php_includes/sidebar_search_flight.php' );
  13. $hotel = getFile( '../php_includes/sidebar_search_hotel.php' );
  14.  
  15. $js = <<<JS
  16. function searchSwitch(DIV) {
  17. switch(DIV) {
  18. case 'package':
  19. package = '<p>Package Loaded</p>';
  20. document.getElementById('package').innerHTML = 'test';
  21. document.getElementById('hotel').innerHTML = '';
  22. document.getElementById('flight').innerHTML = '';
  23. break;
  24. case 'hotel':
  25. hotel = '<p>Hotel Loaded</p>';
  26. document.getElementById('package').innerHTML = '';
  27. document.getElementById('hotel').innerHTML = 'test';
  28. document.getElementById('flight').innerHTML = '';
  29. break;
  30. case 'flight':
  31. flight = '<p>Flight Loaded</p>';
  32. document.getElementById('package').innerHTML = '';
  33. document.getElementById('hotel').innerHTML = '';
  34. document.getElementById('flight').innerHTML = 'test';
  35. break;
  36. default:
  37. alert( 'No search cat selected' );
  38. break;
  39. }
  40. }
  41. JS;
  42.  
  43. $filename = '../js_includes/SearchDiv.js';
  44. $fh = fopen( $filename,'w' ) or die( "Error opening file {$filename}" );
  45. fwrite( $fh,$js ) or die( "Unable to write to file {$filename}" );
  46. fclose( $fh );
  47.  
  48. ?>
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 21st, 2008
0

Re: fwrite not working with ob_start, Any ideas?

Hey kkeith29,

Big thanks for code rewrite but unfortunately it isn't writing the file at all and there are no errors being output.

When run it just shows a blank page, then when I check my js_includes folder it doesn't have the file.

I haven't tried editing any of the code as I'm sort of out of ideas.

I also had a look at what it would take to include the content directly and it isn't nice. I'm stumped.

If you've any further ideas please post but if not many thanks for the help so far.



Richard
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Nov 21st, 2008
1

Re: fwrite not working with ob_start, Any ideas?

make sure that directory is writable. there is nothing else that would stop it from making that file.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 21st, 2008
0

Re: fwrite not working with ob_start, Any ideas?

As much as I can tell it is. The file is being written when output buffering is not on. So I would assume that that means that the directory is OK.

I'm developing locally on a Mac (the folder properties show it not locked and that read & write is allowed) is there any way that could be causing a problem?

If not I'll keep working on it and if I get it I'll make sure to post up the fix.

Thanks again for the help. Really appreciate it.


Richard
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Nov 21st, 2008
0

Re: fwrite not working with ob_start, Any ideas?

So for anyone who has come across this in the future I wanted to say that i found out the problem and let everyone know.

First off thanks for all the help by the guys that posted.

So the problem wasn't caused by the output buffering code, rather it was what I was including. In my include file I had other php statements that did some stuff and then output it with echo.

What I tried was to use output buffering to create a HTML version of the original include file and then use that (HTML) file in the include statement in the above code samples. That seems to have worked. A bit roundabout but gets the job done.

If anyone comes across this in the future and doesn't get what I mean feel free to email rickya100 at g m a i l . c o m.

Or if anyone comes across this and thinks OMFG why did he do all that when he could have just [insert stupidly simple code here], feel free to send the better way of doing it to the above email. And I thank you in advance.
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: can someon explain it to me please
Next Thread in PHP Forum Timeline: assing a value to DEfine





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC