Ok, so what's wrong with this script
Ok, so what's wrong with this script
Author
Discussion

philthy

Original Poster:

4,697 posts

271 months

Tuesday 26th June 2007
quotequote all
Trying to get the latest webcam picture to display using a php script. I've created a folder called "webcam1", which has a load of test .jpg's in it. I can set my cam software to upload using a variable filename ie cam1200706261815 and need the script to find the newest .jpg.
I've got this far on my own, but would really appreciate some pointers.

<?php
$handle1 = fopen("webcam1/", "r"); // skidvd
$data1 = fread($handle1, 1000);
$pamp_start_search = "<img src=\"";
$pamp_s_length = strlen($pamp_start_search);
$pamp_end_search = " border=1 width=640>";
$pamp_e_length = strlen($pamp_end_search);
$pamp_start = (strpos($data1,$pamp_start_search)+$pamp_s_length);
$pamp_end = (strpos($data1,$pamp_end_search)-1);
$pamp_length = ($pamp_end-$pamp_start);
$pamp_txt = mb_strcut($data1,$pamp_start,$pamp_length);
?>
<html>
<head>
<title>Latest pics test</title>
</head>
<body>
<!-- skidvd WEBCAM -->
<p align="center" style="font-family: trebuchet MS; font-size: 12px;"><font size="+3">Skidvd latest webcam pic</font></p>
<div align="center"><img src="<?php echo $pamp_txt ?>">
<!-- END OF skidvd WEBCAM -->
</div>
</body>
</html>


TIA
Phil

zoidberg

8,891 posts

313 months

Tuesday 26th June 2007
quotequote all
Is the problem that the image is not displaying on the webpage or that the script is not finding the correct jpg?

philthy

Original Poster:

4,697 posts

271 months

Tuesday 26th June 2007
quotequote all
It's not displaying. The Pic url shows as http;//www.mysite.co.uk/webcam/ nothing on the end.

zoidberg

8,891 posts

313 months

Tuesday 26th June 2007
quotequote all
So the string $pamp_txt equals "http://www.mysite.com/webcam/" ?

Try echo-ing out all the variables so you can see what is happening at each stage:


<?php
$handle1 = fopen("webcam1/", "r"); // skidvd
$data1 = fread($handle1, 1000);
echo $data1;
$pamp_start_search = "<img src=\"";

$pamp_s_length = strlen($pamp_start_search);
echo $pamp_s_length;

$pamp_end_search = " border=1 width=640>";

$pamp_e_length = strlen($pamp_end_search);
echo $pamp_e_length;

$pamp_start = (strpos($data1,$pamp_start_search)+$pamp_s_length);
echo $pamp_start;

$pamp_end = (strpos($data1,$pamp_end_search)-1);
echo $pamp_end;

$pamp_length = ($pamp_end-$pamp_start);
echo $pamp_length;

$pamp_txt = mb_strcut($data1,$pamp_start,$pamp_length);
echo $pamp_txt;

?>
<html>
<head>
<title>Latest pics test</title>
</head>
<body>
<!-- skidvd WEBCAM -->
<p align="center" style="font-family: trebuchet MS; font-size: 12px;"><font size="+3">Skidvd latest webcam pic</font></p>
<div align="center"><img src="<?php echo $pamp_txt ?>">
<!-- END OF skidvd WEBCAM -->
</div>
</body>
</html>


Edited by zoidberg on Tuesday 26th June 19:37

zoidberg

8,891 posts

313 months

Tuesday 26th June 2007
quotequote all
Actually, thinking further on this, I think what you need to do is:

1. Open the folder with the images in
2. Loop through the image names that are in that folder, and for each one:
2a. Split the name string into "cam1" and the variable date (eg 200706261815) using str_split
2b. Convert the variable date string to an integer (intval($variable_date_string))
2c. The latest picture will be the largest integer in the folder, so each time you find a larger value, set a variable equal to it
3. Finish loop

At the end of this loop, you'll have a variable with the largest variable date value in. Add it to the string "cam1" to make up the filename
4. Display it

{{{<?php

// Open folder with images in using fopen

$latestDate = 0; // Set variable for use later on in the loop

// Start loop
// Get file name
$nameArray = str_split($fileName,4); // The array will contain "cam1" and "200706261815.jpg"

// Split again
$interimString = $nameArray[1]; // This string will be "200706261815.jpg"

$datePartArray = str_split($interimString, 12);

$intDate = intval($datePartArray[0]); // This will get the date part in integer form
if ($intDate > $latestDate)
{
$latestDate = $intDate;
}
// End loop

// You now have the latest date in the variable $latestDate
$latestImage = "cam1" . $latestDate;

echo "<img src='" . $latestImage . ".jpg' />";
}}}

Hope this makes some kind of sense!

Edited by zoidberg on Tuesday 26th June 20:21

philthy

Original Poster:

4,697 posts

271 months

Tuesday 26th June 2007
quotequote all
Lights are coming on Zoidberg, but not all of them yet ;-)

I'll keep trying.
Thanks

TheExcession

11,669 posts

281 months

Tuesday 26th June 2007
quotequote all

philthy

Original Poster:

4,697 posts

271 months

Tuesday 26th June 2007
quotequote all
Getting warmer.....

TheExcession

11,669 posts

281 months

Tuesday 26th June 2007
quotequote all
philthy said:
Getting warmer.....
smile good luck

philthy

Original Poster:

4,697 posts

271 months

Tuesday 26th June 2007
quotequote all
Almost there...

This script brings up the latest image regardless of filename it seems

<?php
function compare($a,$b) {
return $b[1]-$a[1];
}
$dir='webcam1/'; $max=1;
$files=Array();
$f=opendir($dir);
while (($file=readdir($f))!==false) {
if($file!="." && $file!=".." && is_file("$dir/$file&quotwink) {
$files[]=Array($file,filemtime("$dir/$file&quotwink);
}
}
closedir($f);
usort($files,"compare&quotwink;
$m=min($max,count($files));
for ($i=0;$i<$m;$i++) {
print "{$files[$i][0]}\n";
}
?>


Really stupid question time, how do I display it, echo ????????

Cheers.

Edited by philthy on Tuesday 26th June 21:43


Edited by philthy on Tuesday 26th June 21:44

TheExcession

11,669 posts

281 months

Tuesday 26th June 2007
quotequote all
philthy said:
Almost there...

This script brings up the latest image regardless of filename it seems
8<
I really hate reading php wink
Can you post the output of this script?

My guess would be get the script to output just the filename you want and then embed the script into part of the url for the IMG tag.

so something like...

<img src="<?php your script ?>">

HTH
best
Ex


zoidberg

8,891 posts

313 months

Tuesday 26th June 2007
quotequote all
Yeah - if the filename is in a variable called $image, display it like this:

<img src="<?php echo $image; ?>" />

or from within PHP:

<?php

echo "<img src='" . $image . "' />";

?>

philthy

Original Poster:

4,697 posts

271 months

Tuesday 26th June 2007
quotequote all
It's all going blurry now, been looking too long.

The script above I named latest.php When run from the browser, it displays "webcam200.jpg" (my test file). I tried to embed it in a html page using <?php include("latest.php&quotwink; ?>
The image code I tried was this:
<img src="<?php echo latest.php ?>" />

Going to bed before head goes pop.

Timsta

2,779 posts

277 months

Tuesday 26th June 2007
quotequote all
If your script is set to echo the filename then do this:

<img src="<?php include latest.php ?>" />

So latest.php spits out a filename.

Edited by Timsta on Wednesday 27th June 08:19

philthy

Original Poster:

4,697 posts

271 months

Wednesday 27th June 2007
quotequote all
Got it working using <img src="webcam1/<?php include("cam1.php&quotwink; ?>"
Tried to get it to display a second image from webcam2 folder using the same script named cam2.php it didn't like that !
I've got to tinker with my software to see if I can upload different cams to different folders, but you guys have cracked what I needed.
Thanks to you all.