View Single Post
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