| | |
fwrite not working with ob_start, Any ideas?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 78
Reputation:
Solved Threads: 1
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.
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)
<?php //Storing the code to load into each div. Held in other php files to aid code resue ob_start(); // start buffer include ('../php_includes/sidebar_search_package.php'); $package = ob_get_contents(); // assign buffer contents to variable ob_end_clean(); // end buffer and remove buffer contents ob_start(); // start buffer include ('../php_includes/sidebar_search_flight.php'); $flight = ob_get_contents(); // assign buffer contents to variable ob_end_clean(); // end buffer and remove buffer contents ob_start(); // start buffer include ('../php_includes/sidebar_search_hotel.php'); $hotel = ob_get_contents(); // assign buffer contents to variable ob_end_clean(); // end buffer and remove buffer contents $filename = "../js_includes/SearchDiv.js"; $file = fopen($filename,"w"); if(!$file){ echo "Error creating searchDiv2"; exit; } if(!is_writable($filename)){ exit("$filename is not writable"); } //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 $to_write = " function searchSwitch(DIV) { switch (DIV) { case 'package': package = '<p>Package loaded</p>'; document.getElementById('package').innerHTML = 'test'; document.getElementById('hotel').innerHTML = ''; document.getElementById('flight').innerHTML = ''; break; case 'hotel': hotel = '<p>Hotel loaded</p>'; document.getElementById('package').innerHTML = ''; document.getElementById('hotel').innerHTML = 'test'; document.getElementById('flight').innerHTML = ''; break; case 'flight': flight = '<p>Flight loaded</p>'; document.getElementById('package').innerHTML = ''; document.getElementById('hotel').innerHTML = ''; document.getElementById('flight').innerHTML = '<p>Flight Loaded</p>'; break; default : alert('No search cat selected'); } }//Closing function opening "; $to_write = '<p>Test</p>'; $write = fwrite($file, $to_write); if( !$write ){ echo "Content could not be written to the file $filename"; exit; } ?>
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
http://bg2.php.net/manual/en/function.fopen.php
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
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.
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
I miss my mind the most...."
Mark Twain
•
•
Join Date: Mar 2008
Posts: 78
Reputation:
Solved Threads: 1
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):
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.
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)
ob_start(); // start buffer include ('../php_includes/sidebar_search_flight.php'); //$flight = ob_get_contents(); // assign buffer contents to variable //ob_end_clean(); // end buffer and remove buffer contents $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
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.
i also included a function because repetitive code drives me crazy.
PHP Syntax (Toggle Plain Text)
<?php function getFile( $file ) { ob_start(); include( $file ); $data = ob_get_contents(); ob_end_clean(); return $data; } $package = getFile( '../php_includes/sidebar_search_package.php' ); $flight = getFile( '../php_includes/sidebar_search_flight.php' ); $hotel = getFile( '../php_includes/sidebar_search_hotel.php' ); $js = <<<JS function searchSwitch(DIV) { switch(DIV) { case 'package': package = '<p>Package Loaded</p>'; document.getElementById('package').innerHTML = 'test'; document.getElementById('hotel').innerHTML = ''; document.getElementById('flight').innerHTML = ''; break; case 'hotel': hotel = '<p>Hotel Loaded</p>'; document.getElementById('package').innerHTML = ''; document.getElementById('hotel').innerHTML = 'test'; document.getElementById('flight').innerHTML = ''; break; case 'flight': flight = '<p>Flight Loaded</p>'; document.getElementById('package').innerHTML = ''; document.getElementById('hotel').innerHTML = ''; document.getElementById('flight').innerHTML = 'test'; break; default: alert( 'No search cat selected' ); break; } } JS; $filename = '../js_includes/SearchDiv.js'; $fh = fopen( $filename,'w' ) or die( "Error opening file {$filename}" ); fwrite( $fh,$js ) or die( "Unable to write to file {$filename}" ); fclose( $fh ); ?>
•
•
Join Date: Mar 2008
Posts: 78
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Mar 2008
Posts: 78
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Mar 2008
Posts: 78
Reputation:
Solved Threads: 1
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.
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.
![]() |
Other Threads in the PHP Forum
- Previous Thread: can someon explain it to me please
- Next Thread: assing a value to DEfine
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax alerts apache api array beginner binary broken cakephp checkbox class cms code convert cron curl database date directory display download dynamic echo email error file files folder form forms function functions google hack href htaccess html htmlspecialchars image include insert integration ip java javascript joomla limit link login loop mail menu methods mlm mod_rewrite multiple mysql network object oop overwrite parse paypal pdf php problem query radio random recursion redirect regex remote script search securephp server sessions sms soap source space sql structure syntax system table tutorial update upload url validation validator variable video web xml youtube






