function sync_files( $remote, $local )
{
if ( ! is_file( $local ) ) { return copy( $remote, $local ); }
$handle = fopen( $remote, 'r' );
if ( ! $handle ) { die( "Could not open {$remote}." ); }
$meta = stream_get_meta_data( $handle );
fclose( $handle );
foreach( $meta['wrapper_data'] as $response )
{
// Redirection
if ( substr( strtolower( $response ), 0, 10 ) === 'location: ' ) {
return sync_files( substr( $response, 10 ), $local );
}
// Compare sizes
if ( substr( strtolower( $response ), 0, 16 ) === 'content-length: ' )
{
if ( (int) filesize( $local ) !== (int) substr( $response, 16 ) ) {
return copy( $remote, $local );
}
continue;
}
// Compare dates
if ( substr( strtolower( $response ), 0, 15 ) === 'last-modified: ' )
{
if ( (int) filemtime( $local ) < (int) strtotime( substr( $response, 15 ) ) ) {
return copy( $remote, $local );
}
continue;
}
}
return false;
}