Get file length php

PHP: Get the size of a remote file.

In this article, we will show you how to get the size of a remote file using PHP.

We will also include both a cURL and a non-cURL example.

Both of these solutions will attempt to get the size of the file without actually downloading it.

Be warned that there is no surefire way to get the size of a remote file without downloading it. This method relies on the server in question returning a Content-Length header in the header response. Content-Length is an optional header for servers, which means that it might not always be present.

Using cURL to get the size of a remote file.

If you would like to use PHP’s cURL extension to get the size of a remote file, then you can use the following example:

//URL of the remote file that you want to get //the file size of. $remoteFile = 'http://site.com/file.mp4'; //Create a cURL handle with the URL of //the remote file. $curl = curl_init($remoteFile); //Set CURLOPT_FOLLOWLOCATION to TRUE so that our //cURL request follows any redirects. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //We want curl_exec to return the output as a string. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //Set CURLOPT_HEADER to TRUE so that cURL returns //the header information. curl_setopt($curl, CURLOPT_HEADER, true); //Set CURLOPT_NOBODY to TRUE to send a HEAD request. //This stops cURL from downloading the entire body //of the content. curl_setopt($curl, CURLOPT_NOBODY, true); //Execute the request. curl_exec($curl); //Retrieve the size of the remote file in bytes. $fileSize = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD); var_dump($fileSize); //Convert it into KB $fileSizeKB = round($fileSize / 1024); echo 'File is ' . $fileSizeKB . ' KB in size.';

In the code above, we configured cURL to send a HEAD request to the remote file in question.

Читайте также:  Си шарп для маленьких

We then extracted the “Content-Length” header by using the curl_getinfo function and the CURLINFO_CONTENT_LENGTH_DOWNLOAD option.

Note that if the Content-Length header is not returned by the remote server, curl_getinfo will return a -1 (minus one).

You should also note that this is not a reliable way to check if a remote file exists. This is because curl_getinfo might return -1 even if the resource in question exists.

If you want to check if a remote file actually exists or not, then you should read our guide on checking to see if an HTTP resource exists.

Using PHP’s get_headers function to get the size of a remote file.

If you do not have cURL installed, then you can use PHP’s inbuilt get_headers function:

//URL of the remote file that you want to get //the file size of. $remoteFile = 'http://remote-site.com/video.mp4'; //Get the header response for the file in question. $headers = get_headers($remoteFile, 1); //Convert the array keys to lower case for the sake //of consistency. $headers = array_change_key_case($headers); //Set to -1 by default. $fileSize = -1; //Check to see if the content-length key actually exists in //the array before attempting to access it. if(isset($headers['content-length'])) < $fileSize = $headers['content-length']; >var_dump($fileSize);
  1. We fetched the response headers for the resource in question by using PHP’s get_headers function. We passed in 1 as the second parameter. The second parameter tells get_headers to parse the response and set each header as an array key.
  2. To keep things consistent, we converted all array keys to lower case by using the array_change_key_case function.
  3. We set the $fileSize variable to -1 by default. If the $fileSize variable is still -1, we will know that the Content-Length header was not returned by the server.
  4. We check to see if the “content-length” key exists in our $headers array. Failing to use isset in this particular scenario could result in an undefined index warning. As stated above, the Content-Length header is optional.
  5. Finally, we printed the size of the remote file in bytes.

If you are wanting to get the size of a local file, then you should check out our tutorial on how to get a file’s size in PHP.

Источник

PHP: Get file size.

This is a guide on how to get the size of a file using PHP. In this tutorial, we will get the size of a file in bytes using PHP’s filesize function before converting those bytes into KB, MB and GB, which are far more human-friendly.

PHP’s filesize function.

PHP’s filesize function takes in one parameter: A string parameter called $filename, which should contain the path to the file.

Take a look at the following example:

//The path to our file. $file = 'photograph.jpg'; //Get the file size in bytes using PHP's filesize function. $fileSizeBytes = filesize($file); //In my case, the file was 269,708 bytes in size. var_dump($fileSizeBytes);

The code snippet above assumes that the file “photograph.jpg” is located in the same directory as our PHP script.

If our file was located in another directory called images, we could use the following relative path:

//A relative path. $file = '../images/photograph.jpg'; //Get the file size in bytes. $fileSizeBytes = filesize($file);

Note that the filesize function will also accept an absolute path to the file:

//Using an absolute path. $file = 'C:\wamp\www\photograph.jpg'; //Get the file size in bytes. $fileSizeBytes = filesize($file);

If the filesize function is given an incorrect file path, it will throw the following warning:

“Warning: filesize(): stat failed for /path/to/photograph.jpg”

PHP’s filesize function uses the system’s underlying stat command to get the size of the file in question.

Getting the file size in KB.

If you are primarily dealing with images or other small files, you might want to convert the bytes into KB (kilobytes).

//Relative path to our file. $file = 'photograph.jpg'; //Get the file size in bytes. $fileSizeBytes = filesize($file); //Convert the bytes into KB. $fileSizeKB = round($fileSizeBytes / 1024); //269,708 bytes divided by 1024 results in 263 KB var_dump($fileSizeKB);

In the code snippet above, we got the size of the file in bytes and then divided the result by 1024. This is because there are roughly 1024 bytes in every kilobyte.

Getting the file size in MB.

The MB (megabyte) is a useful metric if you are dealing with MP3 files, Zip Files, PDFs or other relatively-large files.

An example of getting a file’s size in MB:

//Path to our file. $file = 'photograph.jpg'; //Get the file size in bytes. $fileSizeBytes = filesize($file); //Convert the bytes into MB. $fileSizeMB = ($fileSizeBytes / 1024 / 1024); //269,708 bytes is 0.2572135925293 MB var_dump($fileSizeMB); //Format it so that only 2 decimal points are displayed. $fileSizeMB = number_format($fileSizeMB, 2); //It now becomes 0.26 MB. var_dump($fileSizeMB);
  1. Got the size of the file in bytes using PHP’s filesize function.
  2. Converted the bytes into MB by dividing the bytes by 1024 twice.
  3. Because the result contains far two many decimal places, we used PHP’s number_format function to limit the number of decimal places to 2.

In my case, the “photograph.jpg” file was 269,708 bytes in size, which became 0.26 MB.

Using PHP to get the file size in GB.

If you are dealing with large files such as videos, you might want to use GB (gigabytes):

//The path to our file. $file = 'large-file.mp4'; //Get the file size in bytes. $fileSizeBytes = filesize($file); //Convert the bytes into GB. $fileSizeGB = ($fileSizeBytes / 1024 / 1024 / 1024); var_dump($fileSizeGB);

In the code sample above, we converted the bytes into GB by dividing the result of filesize by 1024 three times.

filesize won’t work with remote files.

The filesize function will not work with remote files. If you attempt to get the size of a remote file using the filesize function, it will spit out the following warning:

Warning: filesize(): stat failed for http://example.com/file.mp4

This is because the underlying stat command does not support remote files. See: Get the size of a remote file using PHP.

Источник

Оцените статью