Skip to content

Instantly share code, notes, and snippets.

@YOzaz
Last active December 3, 2018 13:31
Show Gist options
  • Save YOzaz/6d3d73330eb28755cd4229018f1b40bf to your computer and use it in GitHub Desktop.
Save YOzaz/6d3d73330eb28755cd4229018f1b40bf to your computer and use it in GitHub Desktop.
Check if image exists and compare file size
<?php
stream_context_set_default( ['http' => ['method' => 'HEAD'] ] );
$head = array_change_key_case( get_headers( $fullremotefilename, 1 ) );
$content_length = isset($head['content-length']) ? (int)( $head['content-length'] ) : 0;
if ( $content_length ) {
$img_size = @filesize( $localfilename );
if ( $img_size !== false && $img_size !== $content_length ) {
// images match!
} else {
// images don't match
}
}
// don't forget to reset default stream settings
stream_context_set_default( ['http' => ['method' => 'GET'] ] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment