ob_clean doesn't clean the output before the h

2019-07-04 02:02发布

问题:

I am facing the issue.I changed the db.php file and unfortunately I put the spaces before and after the php tags.I used this in the image.php file.I am getting the error header already sent.I know that it is because of the space in db.php but I used ob_clean in image.php.

Below is the code.

  spaceishere<?php
  $con=mysql_connect("localhost", "username", "password") or die ('I cannot connect to the           database because: ' . mysql_error());
  mysql_select_db("dbname",$con) or die  ("Cannot Connect to Database");
  ?>andhere  

image.php file contains the code of

<?php
require_once('db.php');
$sql="mysqlquery";
$query = mysql_query($sql); 
$row = mysql_fetch_assoc($query);
ob_clean();
header("content-type:image/jpg") ; 
echo stripslashes($row[imagecolumn]); 
?>

everything is working fine when I removed the spaces in db.php.But I cant able to understand why my ob_clean is not working.

Could you please anyone explain it.And I dont want to change the db.php file.Because I am having problem on it.Please dont ask whats the problem.

I want to done all my changes in image.php to make it work.

Please help on this.

回答1:

You should first start the output buffering by placing a call to ob_start(); before your inclusion.



回答2:

i think that is because you are not in "php"-mode so php does not know that something is within the output buffer, because it only knows the thing which php parses and thats within the php-tags. So in your case the spaces are outside the php tag so php does not know of this and can not "clean" it.