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


//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;
}

?>
Member Avatar for Rhyan

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.

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?

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):

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.

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

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 );

?>

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

make sure that directory is writable. there is nothing else that would stop it from making that file.

commented: Very helpfull. +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

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.

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

Do you sure PHP is displaying errors?

The only thing I can think of is if you have an error occurring in the output buffering part of the code, and it's stopping code execution from moving on to the file writing portion.

Make sure you have error reporting on.

ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');

See if you get errors.

ps: a shortcut for:

$data = ob_get_contents();
	ob_end_clean();

is

$data = ob_get_clean();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.