Jump to content

Rename, delete, display, repeat


donlizard
Go to solution Solved by Psycho,

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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>
width="352" height="288" frameborder="0" scrolling="no">
</iframe>
</body>
</html>

 

Thanks for your time,

Donlizard

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ....
Link to comment
Share on other sites

 

 

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/";
Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

 

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.

Link to comment
Share on other sites

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>
width="640" height="360" frameborder="0" scrolling="no">
</iframe>
</body>
</html>
 
Thanks!
Link to comment
Share on other sites

  • Solution

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>
Link to comment
Share on other sites

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.