...

View Full Version : Is it possible to BACKUP old versions of uploaded (overwritten) files in the server?



damlays
11-14-2012, 05:12 AM
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?

firepages
11-14-2012, 08:15 AM
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
$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

damlays
11-14-2012, 12:33 PM
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
11-14-2012, 12:52 PM
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?

firepages
11-15-2012, 12:49 AM
the folder needs to we writable by the web-server ... this will check...

<?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 />";
}
?>

damlays
11-15-2012, 12:42 PM
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?

firepages
11-16-2012, 01:01 AM
OK, change rename() to copy() (http://php.net/manual/en/function.copy.php)

damlays
11-16-2012, 07:28 AM
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?

firepages
11-16-2012, 08:08 AM
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() (http://www.php.net/manual/en/function.md5-file.php) 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?

damlays
11-16-2012, 08:44 AM
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?

tangoforce
11-16-2012, 02:12 PM
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.

damlays
11-17-2012, 05:26 AM
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?

tangoforce
11-17-2012, 10:28 AM
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.

damlays
11-17-2012, 02:24 PM
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?

tangoforce
11-17-2012, 02:36 PM
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?).



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.

damlays
11-17-2012, 03:32 PM
Thank you for the further information. Of course you can offer, maybe in another project I can request your professional help. But as I wrote, on this subject I will try working only in amateur basis.

If you agree to continue helping me and you can give me some hints (refer/show me some sample codes on the interntet doing this function) as a starting point for me, then I can work on them and try to modify according to my needs. (And I can ask again some little points about the code which I modified -if I can do-)

damlays
11-21-2012, 06:28 AM
it seems no more help ..thank you for the previous helps anyway..

tangoforce
11-21-2012, 02:44 PM
I don't know what you want me to say?

I've offered to write the code for you but instead you prefer to do it yourself, have me give you all the information, you struggle to write the code so get me doing it, then help you fix your code / debug it and all for free?

If someone offers to do something for you for a fair price, it's a bit of a cheek to turn around and then ask them to help you do it while actually expecting them to do most of the work because you don't know how to DIY. You might just as well asked me to do it for free because thats effectively what you're getting at.

On top of that, I've explained the logic to you that you need to write code that loops at least once per second (twice max) that checks the source folder for files and moves them to your destination folder and continues to do so for 59 seconds. Then every minute your cron runs the script again.

I've explained the theory to you of how this process should work. Go away, hit google and find out how to do these small steps bit by bit until you work out what you're doing and can put it all together. It really isn't that hard you just have to make an effort.

Sorry if that sounds harsh but you say you want to do it yourself and rather than pay, so DO IT! Everyone who writes PHP has had to learn the hard way and that takes time and effort. If you then want someone to write your code for you it's only fair that you reward them for it or be prepared to do some learning yourself. This forum is really aimed at helping people out with small snippets of code or helping bug fixes etc not asking for people to either write your complete project for free or help you plan and build it from the ground up. There is a projects forum for that!

Hit google, look up php loops, then php directory scan etc. That will get you started :thumbsup:

damlays
11-22-2012, 02:11 PM
I don't know what you want me to say?

why did you take it for yourself ? :)

this is a big forum that hundreds read the threads, the message was not for you..
and I would like to thank to all prevous helps also..

tangoforce
11-22-2012, 03:34 PM
I think the other people stopped responding quite a long time ago.

Have you made a start yet? - Have you got your loop code and file scanning code done?

damlays
11-22-2012, 03:49 PM
yes responding stopped..but reading continued by users..so I want to put end to the subject with my thanks..

I don't know is it allowed in this forum to use here as market place for professional programmers to catch new customers or not, but I am repeating "nothing was about you in my message"

Forums are sharing places..everybody shares only the amount of knowledge/help what he/she wants to share(or not share)....So I am thanking to all people who helped and not helped..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum