fwrite not working with ob_start, Any ideas?

Thread Solved

Join Date: Mar 2008
Posts: 78
Reputation: rickya100 is an unknown quantity at this point 
Solved Threads: 1
rickya100 rickya100 is offline Offline
Junior Poster in Training

fwrite not working with ob_start, Any ideas?

 
0
  #1
Nov 20th, 2008
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.

  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: fwrite not working with ob_start, Any ideas?

 
0
  #2
Nov 20th, 2008
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
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: fwrite not working with ob_start, Any ideas?

 
0
  #3
Nov 20th, 2008
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.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: fwrite not working with ob_start, Any ideas?

 
0
  #4
Nov 20th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 78
Reputation: rickya100 is an unknown quantity at this point 
Solved Threads: 1
rickya100 rickya100 is offline Offline
Junior Poster in Training

Re: fwrite not working with ob_start, Any ideas?

 
0
  #5
Nov 21st, 2008
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):

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: fwrite not working with ob_start, Any ideas?

 
0
  #6
Nov 21st, 2008
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.

  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 78
Reputation: rickya100 is an unknown quantity at this point 
Solved Threads: 1
rickya100 rickya100 is offline Offline
Junior Poster in Training

Re: fwrite not working with ob_start, Any ideas?

 
0
  #7
Nov 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: fwrite not working with ob_start, Any ideas?

 
1
  #8
Nov 21st, 2008
make sure that directory is writable. there is nothing else that would stop it from making that file.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 78
Reputation: rickya100 is an unknown quantity at this point 
Solved Threads: 1
rickya100 rickya100 is offline Offline
Junior Poster in Training

Re: fwrite not working with ob_start, Any ideas?

 
0
  #9
Nov 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 78
Reputation: rickya100 is an unknown quantity at this point 
Solved Threads: 1
rickya100 rickya100 is offline Offline
Junior Poster in Training

Re: fwrite not working with ob_start, Any ideas?

 
0
  #10
Nov 21st, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC