Page 1 of 1

Comparing remote FTP and local files by date

Posted: Thu Nov 16, 2006 4:36 am
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?

Posted: Thu Nov 16, 2006 4:47 am
by aaronhall
You could also try comparing file sizes.

Posted: Thu Nov 16, 2006 8:12 am
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.

ftp_rawlist()

Posted: Thu Nov 16, 2006 8:37 am
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 )

Posted: Fri Nov 17, 2006 11:16 am
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...