Ok, so what's wrong with this script
Discussion
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.
TIA
Phil
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
It's not displaying. The Pic url shows as http;//www.mysite.co.uk/webcam/ nothing on the end.
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:
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
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!
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
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"
) {
$files[]=Array($file,filemtime("$dir/$file"
);
}
}
closedir($f);
usort($files,"compare"
;
$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.
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"
) {$files[]=Array($file,filemtime("$dir/$file"
);}
}
closedir($f);
usort($files,"compare"
;$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
philthy said:
Almost there...
This script brings up the latest image regardless of filename it seems
8<
I really hate reading php This script brings up the latest image regardless of filename it seems
8<

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
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"
; ?>
The image code I tried was this:
<img src="<?php echo latest.php ?>" />
Going to bed before head goes pop.
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"
; ?>The image code I tried was this:
<img src="<?php echo latest.php ?>" />
Going to bed before head goes pop.
Got it working using <img src="webcam1/<?php include("cam1.php"
; ?>"
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.
; ?>"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.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



good luck