Jump to content

Recommended Posts

Hi all

 

I've spent all night solving this wierd issue - now I'm hoping someone can explain why it's happening.

 

I've written a function for copying and resizing a jpeg and subsequently used it on a page which has a standard dreamweaver script for inserting to a database (which uses a header redirect). If I paste the resize function directly on the page it works fine - but if I 'include' it I get the old headers already sent error. I've solved the header errors using ob_start and ob_clean but this doesn't seem the ideal method. I was hoping that if I could understand why this error was happening in the first place I could find a way of avoiding the use of ob_start entirely. For whatever reason including the function causes it to send headers to the browser, but I don't understand why.

 

Here's the function:

 

function resize_jpg(	$file, // e.g. $_FILES['file']
					$newlocation = "../images/",
					$newwidth = 0,
					$newfilename = 0,
					$quality = 60 ) 
	{
	// check a file has actually been uploaded
		if ( ($file['size'] == 0) || ($file['tmp'] = '') ) {
		global $message;
		$message.= 'error: you haven\'t uploaded a file<br>';
		return $message;
		}
	// check file is a jpg
	$ext = pathinfo($file['name']);
	$ext = $ext['extension'];
		if (($ext != 'jpg')) {
		global $message;
		$message.= 'error: file needs to be a jpg, not '.$ext.'<br>';
		return $message;
		}
	$tempfile = $file['tmp_name'];
	list($width,$height)=getimagesize($tempfile);
	// if a new width hasn't been specified keep original width
		if ($newwidth == 0) {
		$newwidth = $width;
		}
	// if a new filename isn't specified then use original filename
		if (empty($newfilename)) {
		$newfilename = $file['name'];
		}
	// check if file exists 
	if (file_exists($newlocation.$newfilename)) {
		global $message;
		$message.= 'error: file '.$newfilename.' already exists<br>';
		return $message;
		}
	$src = imagecreatefromjpeg($tempfile);
	$newheight=($height/$width)*$newwidth;
	$tmp=imagecreatetruecolor($newwidth,$newheight);
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
	$newfile = $newlocation.$newfilename;
	imagejpeg($tmp,$newfile,$quality);
	imagedestroy($tmp);
	imagedestroy($src);
	return $newfilename;
	}

 

IF anyone can explain what causes the header to be sent - and if there's a way around it I'd be enormously grateful. I've used return $newfilename at the end there so I can then insert the filename into my database.

 

If I need to post any more code please let me know.

 

thanks

JRC

I'd have to see the include file to tell you for sure. I've ran into issues (especially on windows) where I receive this error if there are extra newlines and things in the file. For some reason it treats them as output and thus the error. If you would paste the entire file as it would be displayed in the include file it might provide some more insight.... also check your source (right click -> view source). If for some reason something was funky it may have spit an error message out that didnt get displayed on the page.... and then this caused the error. Could be a lot of stuff, if you provide the include source it should help.

Hi,

 

First of all thanks very much for taking a look at this. Here's the code where I'm calling the function (I've trimmed some of the irrelevant bits so it's not too tedious - I've also attached the two pages in their entirety):

 

<?php require_once('../Connections/andrews_cue.php'); ?>
<?php 
ob_start();
require_once('../_scripts/upload_functions.php'); // this is where the resize_jpg function is
?>
<?php 
// form validation
if (isset($_POST['Submit'])) {

... various bits of form validation...

// check banner size
if ($_FILES['ad_upload']['size'] != 0) {
	// get width and height of uploaded file
	$adFormat = $_POST['adFormat'];
	$uploadedfile = $_FILES['ad_upload']['tmp_name'];
	list($width,$height)=getimagesize($uploadedfile);	

	// set width to 140 for buttons and columns
	if (($width != 140) && ($adFormat != 'banner')) {
		$newwidth = 140;
		} else {
		$newwidth = $width;
		}			

	// set variables for image upload
	$file = $_FILES['ad_upload'];
	$newlocation = "../images/third_party/";
	$newfilename = $file['name'];
	$quality = 80;
	// get filename for database
	$adImg = resize_jpg($file,$newlocation,$newwidth,$newfilename,$quality);
	$HTTP_POST_VARS['adImg'] = $adImg;
	} else {
	$message.= 'You need to upload a file!<br>';
	}
// check headers
ob_clean();
if (headers_sent($filename, $linenum)) {
    $message.= 'headers have been sent in '.$filename.' on line number '.$linenum;
}
// stop form submit if there are error messages
if ($message != '') {
	unset($HTTP_POST_VARS["MM_insert"]);
	//echo $message;
	}
} // end if submit loop
?>

 

The page then progresses through a standard dreamweaver constructed insert statement, which ends with a header:

 

...header(sprintf("Location: %s", $insertGoTo));
...

 

As I mentioned everything works fine if I copy and paste the function into the page itself - the header errors only occur when I include the function. The error always refers to the line after the last line of the function -i.e. the upload_functions.php file currently has 48 lines - the header error will say that headers were sent by line 49 of upload_functions.php - could it be something as simple as me not terminating the function correctly?

 

One final note - the header error only happens when the script processes correctly - i.e. if no image is uploaded, or if it's the wrong format, or if any of my inbuilt error messages are triggered then the header error doesn't happen - I just get my $message back.

 

I'm hoping to use this function throughout my site, so the ideal scenario is to include it rather than paste on every page it's needed.

 

Thanks again!

 

[attachment deleted by admin]

I've been doing some further research in a bid to understand the intricacies of functions and includes - is the error I'm having because including the function is sending data to the buffer, hence the headers already sent error? Is that why the error doesn't happen when the same code is pasted on the page?

 

Is there anything I could change in my function code to prevent this happening - this function will always be used on pages where there'll be a header redirect following a mysql insert.

 

Any help/guidance much appreciated!

J

Okay - I'm realising now that this is quite a common error and have found some bits of code to try out - I've just had plenty of pages before where there's been an include (for database connection for instance) at the top and a header redirect at some point on the page (typically for an insert or update statement) and this is the first time I've had a header error in that situation.

 

I'm still grateful for any guidance or further reading anyone can point me to, but I understand now that this is far from an unusual scenario so no probs if you want to pass this one by.

 

cheers anyway

I DON'T BELIEVE IT!!!!

 

(I'm continuing to update this post just in case any other people experience similar problems)

 

I've found out what the problem was and I'm a complete muppet.

 

There was one single solitary space after the closing php tag ( ?> ) of my included function - I'd been so careful to check for whitespace at the start of the function, on the page itself etc - I just didn't think of checking at the very end.

 

Finally, getting the header error referring to the very last line of my function prompted me to consider moving the line and then I found the space.

 

I freely admit that I'm a twit and have fallen into the commonest of pitfalls - I hope this post helps someone else at some point - just remember to check for whitespace at the end of your includes/functions/whatevers as well as at the beginning!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.