Reading remote file size using file size

I read a guide in which filesize () can calculate the file size from a remote file. However, when I try to do this using the snippet below. I got an errorPHP Warning: filesize(): stat failed for http://someserver/free_wallpaper/jpg/0000122_480_320.jpg in /tmp/test.php on line 5

Here is my snippet:

$file = "http://someserver/free_wallpaper/jpg/0000122_480_320.jpg";
echo filesize( $file );

Turns out I can't use HTTP for file size (). Case is closed. I will use snippet here as a workaround. Solution.

+2
source share
5 answers

filesizedoes not work for HTTP - it depends on statwhich is not supported for the HTTP (S) protocol .

The PHP man page for files states:

Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality.

, / " " filesize. , GET URL- , Content-Length, HEAD-, .

+2

-url,

function getFileSize ($ file) {

   $ch = curl_init($file);
   curl_setopt($ch, CURLOPT_NOBODY, true);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_HEADER, true);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

   $data = curl_exec($ch);
   curl_close($ch);
   $contentLength=0;
   if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
       $contentLength = (int)$matches[1];

   }
   return $contentLength; }
+4

, . filesize stat, HTTP wrapper, , stat PHP.

0

, http_head(). , , , , , . -, , , , .

0
0

Source: https://habr.com/ru/post/1768703/


All Articles