donlizard Posted March 14, 2014 Share Posted March 14, 2014 Hi Everyone, I have literately been out of the game for a few years, but I'm now getting back into it. I have a webcam that FTP's an image up to a server. It's file name is Webcam20140314_160304_7717.jpg and so on. My goal is to rename the newest file to webcam.jpg according to date and time stamp and delete the rest. Then that image would be displayed in an iframe. The script would run every 15 minutes. Any suggestions? I know it's a lot to ask, but I just don't have a clue anymore. Thanks, Donlizard Quote Link to comment Share on other sites More sharing options...
Ansego Posted March 15, 2014 Share Posted March 15, 2014 How is it FTP'ing? Is there code written already? Can you paste in a post? Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 17, 2014 Author Share Posted March 17, 2014 Hi, The camera is ftping the images to the server. There is no code written for this yet. Thanks, donlizard Quote Link to comment Share on other sites More sharing options...
Ansego Posted March 17, 2014 Share Posted March 17, 2014 Hi This might be a start for you (PHP File System): <?php rename("/path/orginial_text_file.txt", "/path/path/etc/your_txt_file.txt"); ?> Ref: http://www.php.net/manual/en/function.rename.php Timer you might want to look at Javascript: <p>A script on this page starts this clock:</p> <p id="demo"></p> <script> var myVar=setInterval(function(){myTimer()},1000); function myTimer() { var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("demo").innerHTML=t; } </script> Ref: http://www.w3schools.com/js/js_timing.asp Regards to your iFrames, should take a look at AJAX and Divs. Hope this points you in the right direction. Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 17, 2014 Author Share Posted March 17, 2014 Hi Ansego, Thank you so much for your input! I'm using the following script to delete all the files every 15 mins, and it works. <?php $dir = './images/'; $days = 900; // 15 mins if($handle = opendir($dir)) { /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { if ( filemtime($dir.$file) <= time()-$days) { unlink($dir.$file); } } closedir($handle); } But I want the script to always leave behind the newest one so I can rename it. The customer is running a Joomla website so he wants to use an iframe, I would love to use a div, but this is what I have to contend with. Slowly but surely I guess .... Thanks, Donlizard Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 17, 2014 Share Posted March 17, 2014 (edited) How much usage are you expecting this page to get? Are you expecting a lot of users to be accessing the frame many times a day or will this page get limited usage? If you are just using this for personal use and it will not get a lot of usage, I wouldn't bother with creating a scheduled process at all. Instead, I would create a process when access the page to check if there are any newly uploaded images and, if so, get the most recent image and rename it. So, let's say this is the page that you would display in the iframe: <html> <body> <img src="webcam.jpg" /> </body> </html> Just put some PHP code at the top to detect if there were any new uploads. If so, find the most recent and rename to the name/location you want and then delete the others. <?php //Define locations for uploads and final file $path_to_uploads = "uploads/"; $final_image = "images/webcam.jpg"; //Path to save the most recent //Get the files in the upload directory $uploaded_files = glob("{$path_to_uploads}*"); //Create var to detect recent files $recent_date = 0; //Iterate through all uploads foreach($uploaded_files as $file) { $file_date = filectime($file); //If file is newer, rename it else delete it if($recent_date < $file_date) { rename($recent_file, $final_image); $recent_date = $file_date; } else { unlink($file); } } ?> <html> <body> <img src="<?php echo $final_image; ?>" /> </body> </html> Edited March 17, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 17, 2014 Author Share Posted March 17, 2014 HI Psycho, You're right, the image will change every 5 or 15 minutes, it really doesn't matter, and yes it's not going to be a high traffic website. That's a great idea about access and renaming! Here's the code to show the image. <html> <head> <title>Webcam</title> </head> <body> <iframe src="http://www.blahblah.com/webcam.jpg" width="352" height="288" frameborder="0" scrolling="no"> </iframe> </body> </html> Thanks for your time, Donlizard Quote Link to comment Share on other sites More sharing options...
Ansego Posted March 17, 2014 Share Posted March 17, 2014 If that does not do what you want try : DirectoryIterator to sort through the files. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 17, 2014 Share Posted March 17, 2014 @donlizard, The code I posted was a working example. Just change the values of the two variables at the top pf the script and then change the HTML at the bottom to what you want. Note: the script assumes that the files in the upload directory are ONLY the ones being uploaded by the webcam. It will process all the files in that directory - either moving them to be the new current image or deleting them. Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 17, 2014 Author Share Posted March 17, 2014 Thank you Psycho and Ansego! Your input has been great! The last time I was doing code like this was in 2006, so getting back into has been a bit of a learning curve. The upload directory for the images is one and the same, the images directory. I'll alter the php and html code and let you know, thanks again. Donlizard Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 17, 2014 Author Share Posted March 17, 2014 Hi Guys, I would assume I have the pathing correct, but it's doing nothing to the images. <?php //Define locations for uploads and final file $path_to_uploads = "./images"; $final_image = "./images/webcam.jpg"; //Path to save the most recent //Get the files in the upload directory $uploaded_files = glob("{$path_to_uploads}*"); //Create var to detect recent files $recent_date = 0; //Iterate through all uploads foreach($uploaded_files as $file) { $file_date = filectime($file); //If file is newer, rename it else delete it if($recent_date < $file_date) { rename($recent_file, $final_image); $recent_date = $file_date; } else { unlink($file); } } ?> Hmmmm .... Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 17, 2014 Share Posted March 17, 2014 The upload directory for the images is one and the same, the images directory. That will not work for two reasons. 1) If there are other images in that directory that are used for the site they will end up getting deleted. 2) After determining the most current image, the logic will delete the current webcam.jpg then replace it with the current image. But, if there are no new uploads, the webcam.jpg IS the most current image. So, the logic would delete that image and then try to rename the most current image (which would have just been deleted!). You should create a separate folder just for the purpose of uploading the images to. As far as your paths, where is the images folder in relation to where the php file is located? In my tests the php file was in the same folder as the images and uploads folders. [Site_Root_Folder] | | - [ImageTest] | - testpage.php | - [images] | - [uploads] For my values I just defined the path starting with the folder name with nothing before it. E.g. $path_to_uploads = "uploads/"; Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 18, 2014 Author Share Posted March 18, 2014 Hi Psycho, I understand everything you have showed and told me, thanx again. I setup the structure like you said, and the paths, unfortunately it is not working. If you go to this URL it will show you what's happening. http://www.cypresshills.com/procomm/ I've tried several different pathing methods, but alas I can't get it working. Thank you, Don Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 18, 2014 Share Posted March 18, 2014 Warning: rename(,images/webcam.jpg) [function.rename]: No such file or directory in /home/cypress/www/www/procomm/index.php on line 23 The above error message is what displays when I go to that page. Why is there a comma in the path? You've not stated the location of the images or uploads directories, nor have you shared what you are currently using in the code for those two locations. So, not much we can do to help at this point. Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 18, 2014 Author Share Posted March 18, 2014 Hi, Here is the code I used for my index.php. Its the same you gave me, with a small added part at the top. <?php ini_set('display_errors', 1); error_reporting(E_ALL ^ E_NOTICE); ?> <?php //Define locations for uploads and final file $path_to_uploads = "uploads/"; $final_image = "images/webcam.jpg"; //Path to save the most recent //Get the files in the upload directory $uploaded_files = glob("{$path_to_uploads}*"); //Create var to detect recent files $recent_date = 0; //Iterate through all uploads foreach($uploaded_files as $file) { $file_date = filectime($file); //If file is newer, rename it else delete it if($recent_date < $file_date) { rename($recent_file, $final_image); $recent_date = $file_date; } else { unlink($file); } } ?> <html> <head> <title>Webcam Test</title> </head> <body> <iframe src="http://www.cypresshills.com/procomm/images/webcam.jpg" width="640" height="360" frameborder="0" scrolling="no"> </iframe> </body> </html> Thanks! Quote Link to comment Share on other sites More sharing options...
Ansego Posted March 18, 2014 Share Posted March 18, 2014 @ donlizard Is it solved? use solved button on the most helpful post to a solution, if all is working. Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted March 18, 2014 Solution Share Posted March 18, 2014 I have no idea what your problem may be. The code worked for me when I tested it. The comma in the error message definitely looks out of place, but I don't see anything in the code to account for it. The only advice I can offer is to add some debugging to the script to see what is and is not happening. EDIT: I do see one problem in that the code should delete the previous file before renaming a new one. But, based on your error messages that isn't the problem. <?php ini_set('display_errors', 1); error_reporting(E_ALL ^ E_NOTICE); //Define locations for uploads and final file $path_to_uploads = "uploads/"; $final_image = "images/webcam.jpg"; //Path to save the most recent //Get the files in the upload directory $uploaded_files = glob("{$path_to_uploads}*"); echo "<b>Debug:</b> Found " . count($uploaded_files) . " files in '{$path_to_uploads}'<br>\n"; //Create var to detect recent files $recent_date = 0; //Iterate through all uploads foreach($uploaded_files as $file) { $file_date = filectime($file); echo "<b>Debug:</b> File: '$file', Date: {$file_date}<br>\n"; //If file is newer, rename it else delete it if($recent_date < $file_date) { echo "<b>Debug:</b> File is newer than previous image. Delete prev. image and rename the file<br>\n"; if(is_file($final_image)) { unlink($final_image); } rename($recent_file, $final_image); $recent_date = $file_date; } else { echo "<b>Debug:</b> File is NOT newer than previous file. Delete the file<br>\n"; unlink($file); } } ?> <html> <head> <title>Webcam Test</title> </head> <body> <iframe src="http://www.cypresshi...ges/webcam.jpg" width="640" height="360" frameborder="0" scrolling="no"> </iframe> </body> </html> Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 18, 2014 Author Share Posted March 18, 2014 What's happening now is that the script deletes the webcam.jpg in the images directory, but is not replacing it with one from the uploads directory and deleting the oldest ones. We're getting closer!! Quote Link to comment Share on other sites More sharing options...
donlizard Posted March 21, 2014 Author Share Posted March 21, 2014 HI Guys, After pondering over the script I changed one small line and the script works great! Yaaaay! Thank You's and kudos go out to Psycho and Ansego for the code and suggestions for my issue, you guys are great! Donlizard Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.