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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tee_Hays
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2011 6:58 am

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

Post 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
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

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

Post 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).
Tee_Hays
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2011 6:58 am

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

Post by Tee_Hays »

Thanks for the feedback, but that's not really helping with the specific problem.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Tee_Hays
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2011 6:58 am

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

Post by Tee_Hays »

Cheers for the update. I got no message like that, but your explanation is very helpful.

Cheers
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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.
Tee_Hays
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2011 6:58 am

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

Post 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
Post Reply