Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-14-2012, 05:12 AM   PM User | #1
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
Is it possible to BACKUP old versions of uploaded (overwritten) files in the server?

For example in my web server, there are 4 webcam images in one folder (continiously updated by overwriting according

to the upload coming from webcam software - as "a.jpg, b.jpg, c.jpg, d.jpg)



Webcam software is always overwriting the image files but I want to keep and save all uploaded images

with increasing numbered file names

(as a0001.jpg, a0002.jpg, a0003.jpg, .... b0001.jpg, b0002.jpg, b0003.jpg, .... for every webcam image

file)

in a seperate folder (by using php or cron job?) while keeping their upload date and time information.



I dont have knowledge in scripting language , but I can guess that there may be some way to do it by PHP or CRON JOB?

Last edited by damlays; 11-14-2012 at 05:15 AM..
damlays is offline   Reply With Quote
Old 11-14-2012, 08:15 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,896
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
you could set a cron job to do that for you, PHP might be something like ...

cron /usr/bin/php -q /path/to/script.php
PHP Code:
<?php
$src_folder
='/home/blah/webcam';
$dest_folder='/home/blah/webcam/history';
$arr=array('a','b','c','d');
foreach(
$arr as $a){
 if(
file_exists("$src_folder/{$a}.jpg")){
    
rename("$src_folder/{$a}.jpg","$dest_folder/{$a}_".date('ymdhis').".jpg");
 }
}
?>
you could do the same with bash on linux of course rather than calling php, whichever you are more comfortable with
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

Last edited by firepages; 11-14-2012 at 08:18 AM..
firepages is online now   Reply With Quote
Old 11-14-2012, 12:33 PM   PM User | #3
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
Thank you for your help.

When I tried to establish your solution I couldn't succeed and I thought that maybe cron job is not right way of doing this?

Because in the host control panel, there is cron job function with the triggering according to time (minimum every minute is possble)

But I need the file will be backed up triggered by every new upload.

Is there any way doing this? (Or am I missing some points?)
damlays is offline   Reply With Quote
Old 11-14-2012, 12:52 PM   PM User | #4
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
By the way now I tried the code without cron.. I made cam.php file and put to the same folder where image files sits;

<?php
$src_folder='/public_html/.mydomain/cam';
$dest_folder='/public_html/.mydomain/cam/backup';
$arr=array('cam_office1','cam_office2a','cam_office2b','cam_home');
foreach($arr as $cam_office1){
if(file_exists("$src_folder/{$cam_office1}.jpg")){
rename("$src_folder/{$cam_office1}.jpg","$dest_folder/{$cam_office1}_".date('ymdhis').".jpg");
}
}
?>

I've created the folder "backup"

After that from web browser I open http://example.com/cam/cam.php

This must write images files to "backup" folder, is it right?
Nothing is written to the new folder?

Last edited by damlays; 11-14-2012 at 01:43 PM..
damlays is offline   Reply With Quote
Old 11-15-2012, 12:49 AM   PM User | #5
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,896
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
the folder needs to we writable by the web-server ... this will check...
PHP Code:
<?php
$src_folder
='/public_html/.mydomain/cam';
$dest_folder='/public_html/.mydomain/cam/backup';
$arr=array('cam_office1','cam_office2a','cam_office2b','cam_home');
if(!
is_writable($dest_folder)){
    die(
"$dest_folder not writable");
}
if(
file_exists("$src_folder/{$cam_office1}.jpg")){
    
rename("$src_folder/{$cam_office1}.jpg","$dest_folder/{$cam_office1}_".date('ymdhis').".jpg");
}else{
   echo 
"can't find file {$src_folder}/{$cam_office1}.jpg<br />";
}
?>
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

Last edited by firepages; 11-15-2012 at 12:52 AM..
firepages is online now   Reply With Quote
Old 11-15-2012, 12:42 PM   PM User | #6
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
Folder was not writeable but I could solve that problem...

rename("$src_folder/{$cam_office1}.jpg","$dest_folder/{$cam_office1}_".date('ymdhis').".jpg");
is MOVING the file to the destination folder,

Is there any other command instead this, which can "COPY" the file with renaming, but without touching the file sitting in the original location?
damlays is offline   Reply With Quote
Old 11-16-2012, 01:01 AM   PM User | #7
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,896
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
OK, change rename() to copy()
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is online now   Reply With Quote
Old 11-16-2012, 07:28 AM   PM User | #8
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
Thank you very much....now we have a php file which makes the copy of all images..this was a great help for me..


If I am not taking your time much, I want to ask your idea about the unsolved part of the solution as well..
Now I am triggering this php file manually by browser or I can make a cron job for every minute..


But both of them are not good solutions, because
1)If there is no change (new upload according to the dedected motion) in camera image file, it is useless to save a copy of image in every minute
2)And also, in one minute there can be 10 different uploads but I can catch and copy only 1 image in this time period by this way

So is there any other ways possible to run this php triggered by every new uploads, or is it impossible to do that?
damlays is offline   Reply With Quote
Old 11-16-2012, 08:08 AM   PM User | #9
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,896
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Quote:
Originally Posted by damlays View Post
So is there any other ways possible to run this php triggered by every new uploads, or is it impossible to do that?
not impossible but would require root access to your server & it ain't straight-forward, it requires inotify-tools on linux so perhaps google for that if you are feeling brave

The obvious alternative is CRON, add to your crontab ... check the php path with `which php`
/usr/bin/php -q /full/path/to/yourscript.php

I am not sure how your images get put on the server, perhpas your cameras have some notify action ? possibly they can send an email or call a webpage when they save the image... need more info there though.

as for not saving the same image file several times you could use md5_file() to generate a hash of the last image moved, save that somewhere (db or text-file) then check the md5() of the image to make sure it has changed b4 you do your next move?
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is online now   Reply With Quote
Old 11-16-2012, 08:44 AM   PM User | #10
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
I see. For entering inotify-tools on linux I don't think so I am brave enough

About md5_file() , I think I understand your point, php file will check this information every time before saving a new file and if it is the same file it will not save..right?
I can guess that it is easy script for you, but it seems a little bit advanced programming for me
I am using iSpy security software for uploading image files, uploads on motion detection, but I couldn't find sending email or caling a web page function paralel with upload..

But I can ask an easier script help from you maybe be the solution of this problem.
If we can modify the current code for saving the image files every hour to a new created folder instead of one stable folder, then it can be possble to manage many saved files.

I mean for every day one parent folder of the day will be created automaticly by php file if it is the first file saving after 12:00 am of previous day. Folders as below
121116
121117
121118
.......

And in each folder, 24 folders will be created one by one by php file when the time comes to save an image if file belongs to a new hour, as
00
01
02
04
....
...
22
23

Is it an easy and short modification or difficult and long?

Last edited by damlays; 11-16-2012 at 08:54 AM..
damlays is offline   Reply With Quote
Old 11-16-2012, 02:12 PM   PM User | #11
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by damlays View Post
But I need the file will be backed up triggered by every new upload.
How is the file uploaded? - FTP or a HTTP upload?

If its FTP then the most you're going to get is a cycling script that cycles for say 59 seconds before quitting and being restarted by a cron.

If you have total control of the system and can run scripts indefinitely then you could write a service that will just run continually checking for new files and saving them otherwise you're stuck with the 59 seconds and a new cron instance scenario.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 11-17-2012, 05:26 AM   PM User | #12
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
How is the file uploaded? - FTP or a HTTP upload?

If its FTP then the most you're going to get is a cycling script that cycles for say 59 seconds before quitting and being restarted by a cron.
.
Software is uploading with ftp. And I dont have the full control of the system.
But what is cycling here, can you explain that some more?
damlays is offline   Reply With Quote
Old 11-17-2012, 10:28 AM   PM User | #13
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Cycling = looping.

Most servers have a php script maximum execution time of either 30 or 60 seconds. Now if you have a 60 second max then you could easily loop a script for 59 seconds (with a sleep between each loop to stop your host shutting you down for CPU hogging). That means that every time the script loops it will check for any files in directory A and copy them into directory B with a new name.

On the 60th second your cron triggers a new version of the script to run and the whole process starts again for another 59 seconds.

If you're unsure of how to set this up, I can do the initial checks of your environment for you (checking max exec time etc) to see if its suitable but if you want the code written by me it would be chargeable.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 11-17-2012, 02:24 PM   PM User | #14
damlays
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
damlays is an unknown quantity at this point
I understand.

If that code is that much complex to be chargable I will not try that way. Because this thing which I am trying is a completely amateur effort for me, and the purpose of posting in this forum is for seeking some simple and amateur helps only. Thank you.

In any way, I wonder about is it possible that php script can understand a new file is uploaded while looping? Is it about using the same technique related with md5_file() which "firepages" told in previous posts?
damlays is offline   Reply With Quote
Old 11-17-2012, 02:36 PM   PM User | #15
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by damlays View Post
If that code is that much complex to be chargable I will not try that way. Because this thing which I am trying is a completely amateur effort for me, and the purpose of posting in this forum is for seeking some simple and amateur helps only. Thank you.
Don't be put off. It isn't overly complex so it's within your grasp, I was just offering to get you up and running asap for a fee (I've been out of work for 2 years so can't blame me for tryin eh?).

Quote:
Originally Posted by damlays View Post
In any way, I wonder about is it possible that php script can understand a new file is uploaded while looping? Is it about using the same technique related with md5_file() which "firepages" told in previous posts?
Yes. It would be able to recognise a different file to the last one by checking the MD5 as firepages suggests. An MD5 checksum is basically a 32bit string comprising of letters and numbers which is basically generated through voodoo and creates a digital fingerprint of a file. It's extremely rare for two files to ever share the same MD5 so that is how you could determine if the file is the same or different to the last one moved. Either way, assuming you're moving the file from A=>B then it would no longer be at the source anyway so ths script would just look for new files and move them.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Reply

Bookmarks

Tags
cron job webcam

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:54 AM.


Advertisement
Log in to turn off these ads.