Get remote file size using Curl

Uses curl and HEAD request to get remove file size without downloading it. Read more.

php
fork add to bundle source
function curl_get_file_size( $url ) { $result = -1; $curl = curl_init( $url ); curl_setopt_array($curl, [ CURLOPT_NOBODY => true, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true ] ); $data = curl_exec( $curl ); $info = curl_getinfo($curl); curl_close( $curl ); if( $data ) { $content_length = "unknown"; $status = $info['http_code']; if( preg_match( "/Content-Length: (\d+)/misu", $data, $matches ) ) { $content_length = (int)$matches[1]; } if( $status == 200 || ($status > 300 && $status <= 308) ) { $result = $content_length; } } return $result; }

usage

load_function('curl_get_file_size'); echo curl_get_file_size('https://function.codes/favicon.ico'); // 15086
← how to use this in app?
create log ...inout