[ELECTRON] PHP - question. . . webcam script...

Ben Rush orionrush at gmail.com
Fri Apr 2 23:15:54 UTC 2010


Hello folks,
Im going to preface this by stating I'm a self described noob, so if
this seems elementary - I stand by my assertion.
I have come to a block and Im not sure how to deal with it, and any
thought would be amazing.

Im working on a fetch image script for a webcam and everything seemed
hunky-dorry.

I later decided to add an if/else on the off chance the camera wasn't
available. This is where things got confusing.

If the camera fails I get a "Warning: Cannot modify header
information". The thing is I cant figure out were the header info is
being declared, as the warning seem to indicate that the declaration
comes from in the 'if' statement itself. Wait didn't that just fail?!?

Moving the 'header('content-type: image/jpg');' higher up in the
script doesn't seem to fix it. Moving it before the if statement all
together gives a broken image link - the echo of file contents don't
seem to make to the browser.

I also checked to see if there was any empty space before or after my
<? ?> - no empty echoes there either. .

totally confused . . . .see demos below

Many thanks,
ben r

Two live demos
http://www.astonesthrowaway.co.uk/webcam/get_image4.php
http://www.astonesthrowaway.co.uk/webcam/get_image4failed.php

<?
//Server details
//address - where this php script is saved. . .
$server_url = 'http://www.astonesthrowaway.co.uk/webcam/';
//Path to save image to
$copydir = "latest_image/"; //directory to copy to (must be CHMOD to 777)

//Webcamera details
//address - where the image is on the camera
$camera = "http://64.142.78.154/jpg/";
//$camera = "http://64.142.78.154/jpg/broken/"; // un-comment out
these lines to test failed camera connection. . .

//Name of image created by webcamera
$image_name = 'image.jpg';

/* ----- no variables to complete below here ------- */

//Assemble the webcamera image location
$img_remote = ( $camera . $image_name );

//Assemble the server image location
$img_latest = ( $server . $copydir . $image_name );

//Assemble the server directory path
$server_local = ( $server . $copydir );

$creation_date = date ("F-d-Y-H:i:s", filemtime($img_latest));

// //Test if cameras is availble by trying to open it for read only
if ( fopen($img_remote, "r")== true ) {

	// test if the camera image already exists on the server and then
rename it with a creation date timestamp
	file_exists($img_latest);
	rename( $img_latest, $server_local . $creation_date .'.jpg');
		
	//Get the remote image display it, and write it to the server
	$data = file_get_contents($img_remote);

		// Spit out the image from memory to the browser
		//Should this be the last thing done or can we do it early to speed
up the end user experience. . . ?
		header('content-type: image/jpg');
		echo $data;
	
		// Write it to the server - ie no time date stamp
		//DO I NEED TO USE flock()? if so how?
		//will it prevent overwriting from other users?
		$file = fopen($copydir . $image_name, "w+");
		fputs($file, $data);
		fclose($file);
		fclose($img_remote);
	
} else {

//problems here - if the webcamera is unavailable am getting
// Warning: Cannot modify header information - headers already sent by
(output started at
/f5/astonesthrowaway/public/webcam/get_image4.php:33) in
/f5/astonesthrowaway/public/webcam/get_image4.php on line 65

//Spit out last image saved to the browser . . .
header('content-type: image/jpg'); //ive tried removing it, moving it
into the 'if' statement with no luck.
// moving it above the if - hints the image - but does not seem to
deliver the image
$get_latest = file_get_contents($img_latest);
echo $get_latest;

}
?>



More information about the members mailing list