Page 1 of 1

Lose images when function in seperate file, using 'require'.

Posted: Fri Jul 08, 2011 7:18 am
by Tee_Hays
Hello all,

I could really use some help with this, I've looked for a long time for a solution.

I have a function which connects to my database, I keep it in a separate file with the rest of my functions.

Code: Select all

function spanDataConnect ($database){
	$con = mysql_connect("localhost","user","password");
	if (!$con){die('Could not connect: ' . mysql_error());}
	mysql_select_db($database, $con);
	return($con);
}
I also have a piece of view code (view.php) which takes image data from a database and renders it. I've removed the error handling elements for brevity.

Code: Select all

$con = spanDataConnect('database');
echo "Connection = ".$con;
$query  = sprintf('select size, type, message, filename from letters where message_ID = %d', $id);
$result = mysql_query($query, $con);
if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $query;
  die($message);
} 
$image = mysql_fetch_array($result);
header('Content-type: ' . $image['type']);
header('Content-length: ' . $image['size']);
echo $image['message'];
The images display fine when the function is in the same file as the view code, but if I use require or include, the images don't display. It's almost like the loading of the 'require' file is taking too long. I get no errors or warnings and I have

Code: Select all

error_reporting(E_ALL)
turned on.

Any ideas on why this may be happening?

Cheers

Tee

Re: Lose images when function in seperate file, using 'requi

Posted: Fri Jul 08, 2011 7:50 am
by oscardog
My want to add

Code: Select all

ini_set('display_errors', 1);
Just before your error_reporting(E_All); line (ideally right at the top of the script).

Re: Lose images when function in seperate file, using 'requi

Posted: Mon Jul 11, 2011 2:27 pm
by Tee_Hays
Thanks for the feedback, but that's not really helping with the specific problem.

Re: Lose images when function in seperate file, using 'requi

Posted: Mon Jul 11, 2011 2:47 pm
by AbraCadaver
Tee_Hays wrote:Thanks for the feedback, but that's not really helping with the specific problem.
Sure it would. It would probably tell you that headers can't be sent, that headers were already sent at this line: echo "Connection = ".$con;

Re: Lose images when function in seperate file, using 'requi

Posted: Mon Jul 11, 2011 2:55 pm
by Tee_Hays
Cheers for the update. I got no message like that, but your explanation is very helpful.

Cheers

Re: Lose images when function in seperate file, using 'requi

Posted: Mon Jul 11, 2011 2:57 pm
by Weirdan
This kind of problem is often caused by extra whitespace before opening <?php tag (or after closing ?> tag) in the included file. For this reason it's useful to omit closing ?> altogether. Also make sure your editor does not add byte order mask (BOM) character when saving your file.

Any output (including your debugging echo statement, but also any error output) in image generating script would also cause image to not be displayed correctly. This means, contrary to what oscardog suggested, that you actually need display_errors to be set to off in any image generating script and either install your custom error handling or rely on errors being written to a log file.

Check actual request/response content in Firebug's Network tab - the problems I mentioned are usually very obvious when you see raw response.
It would probably tell you that headers can't be sent,
Not if the output buffering is turned on.

Re: Lose images when function in seperate file, using 'requi

Posted: Mon Jul 11, 2011 3:03 pm
by Tee_Hays
Yep, thanks both AbraCadaver and Weirdan; it was a noobie error with headers already being sent, I'm sure of it. My functions script has a lot of error checking output in it and I'm sure this is what has caused the problems.

Consider this issue now resolved!

Cheers

Tee