Comparing remote FTP and local files by date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Comparing remote FTP and local files by date

Post by ska »

Hi all,

Here's an issue I'm struggling with. I need to FTP into a remote sever via PHP and then grab all the latest images that are in a directory there. I've written a script that will go through all the images in a directory and download them to a directory on the local server. It will compare filenames at the moment and if the file already exists on the local server, it doesn't bother downloading it again to save bandwidth (this script runs ever hour on Cron and there's several hundred images in there so it's an important consideration).

HOWEVER, it looks like I'm going to have to be a bit more clever than just checking filenames, as images on the remote server are changing but keeping the same filenames. Therefore, an image gets updated on the remote server but not on my local one. Only way I can think of doing this is to check the date of the files on the remote server, and if it has changed since the last sync, the file needs to be downloaded. Something like;

Code: Select all

$ftp=ftp_connect($ftp_ip);
ftp_login($ftp,$ftp_username,$ftp_password);

// thumbails images
ftp_chdir($ftp, "images");
$contents = ftp_nlist($ftp, ".");

$num_of_images=count($contents);

for ($i=0; $i<=$num_of_images; $i++)
{

// check date of remote file

// check date we have stored for this file on the last sync

// if remote_date!=local_date, get file and store remote date again

}
The problem is, I don't if or how you get the date of a file via FTP...? Any ideas anyone? Or any better ways of doing this?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You could also try comparing file sizes.
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post by ska »

Thanks,

If I use file sizes presumably I'd have to do:

Code: Select all

if filesize_remote!=filesize_local
get file
This will be depending on an image that has not changed having exactly the same filesize as when it was last checked. There's no weird variations by a few bytes here and there that could occur when checking a file at different times?

If so, this might be a good way to do it.
User avatar
zeek
Forum Commoner
Posts: 48
Joined: Mon Feb 27, 2006 7:41 pm

ftp_rawlist()

Post by zeek »

Try ftp_rawlist(). Read the comments at the bottom of the page, as there are some good functions for formatting the data at ( http://us2.php.net/manual/en/function.ftp-rawlist.php )
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post by ska »

Thanks, used file size!

Code: Select all

$res = ftp_size($ftp, $server_file);
	
	
		// find out if this file is in the current image list
		$sql="SELECT filesize FROM image_list WHERE imagename='".$filename."'";
		$result = mysql_query( $sql );
		
		
		if (mysql_num_rows($result)>0)
		{
			$row = mysql_fetch_assoc( $result );
			extract($row, EXTR_OVERWRITE );
			
			if ($filesize!=$res)
			{
                          // get file

// etc...
Post Reply