Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,877 questions

51,801 answers

573 users

How to get remote file size from specific URL in PHP

1 Answer

0 votes
function get_file_size( $url ) {
    $curl = curl_init( $url );

    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $info = curl_exec($curl);
    curl_close($curl);

    if (preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $info, $matches)) {
        $HTTP_status_code = (int)$matches[1];
    }

    if (preg_match("/Content-Length: (\d+)/", $info, $matches)) {
      $file_size = (int)$matches[1];
    }

    $result = -1;
    if ($HTTP_status_code == 200 || ($HTTP_status_code > 300 && $HTTP_status_code <= 308)) {
      $result = $file_size;
    }
    
    return $result;
}


$url = 'https://coupondiscountblog.com/images/xcg.jpg';

$file_size = get_file_size($url);

echo $file_size . " bytes";


  
/*
run:
          
1878 bytes

*/

 



answered Sep 21, 2019 by avibootz

Related questions

1 answer 228 views
3 answers 310 views
1 answer 152 views
2 answers 1,099 views
1 answer 206 views
3 answers 227 views
227 views asked Sep 21, 2019 by avibootz
...