What are the limitations of using PHP to determine file sizes on remote servers?
When using PHP to determine file sizes on remote servers, one limitation is that the function `filesize()` may not work for remote files accessed via HTTP or FTP. To overcome this limitation, you can use cURL to fetch the file size from the remote server headers.
function get_remote_file_size($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
return $size;
}
$remote_file_url = 'http://example.com/file.txt';
$file_size = get_remote_file_size($remote_file_url);
echo 'File size: ' . $file_size . ' bytes';
Keywords
Related Questions
- What are the benefits of using arrays to generate random strings in PHP?
- What are some potential challenges when trying to save and transfer data offline in PHP?
- How can PHP be used to dynamically include files from different domains based on specific conditions, and what are the potential pitfalls to be aware of?