Skip to content

Instantly share code, notes, and snippets.

@augustyip
Last active December 23, 2015 19:29
Show Gist options
  • Save augustyip/8b14e2db3d226a32f6dc to your computer and use it in GitHub Desktop.
Save augustyip/8b14e2db3d226a32f6dc to your computer and use it in GitHub Desktop.
CHECK REMOTE FILE EXISTS
<?php
// CHECK REMOTE FILE EXISTS
function remote_file_exists( $url_file ) {
$url_file = trim( $url_file );
if ( empty( $url_file ) )
return FALSE;
$url_arr = parse_url( $url_file );
if ( !is_array( $url_arr ) || empty( $url_arr ) )
return FALSE;
$host = $url_arr['host'];
$path = $url_arr['path'] . "?" . $url_arr['query'];
$port = isset( $url_arr['port'] ) ? $url_arr['port'] : "80";
$fp = fsockopen( $host, $port, $err_no, $err_str, 30 );
if ( !$fp )
return FALSE;
$request_str = "GET " . $path . " HTTP/1.1\r\n";
$request_str .= "Host:" . $host . "\r\n";
$request_str .= "Connection:Close\r\n\r\n";
fwrite( $fp, $request_str );
//fread replace fgets
$first_header = fread( $fp, 128 );
fclose( $fp );
if ( trim( $first_header ) == "" )
return FALSE;
//check $url_file "Content-Location"
if ( !preg_match( "/200/", $first_header ) || preg_match( "/Location:/", $first_header ) )
return FALSE;
return TRUE;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment