PHP Gets the remote file size and information function (header header information gets)

Source: Internet
Author: User
Tags empty file size header rar trim
function | file size | remote file |header

PHP Gets the remote file size and information function (header header information gets)

Arisisi alixixi.com Development team to do a customer system, need to do remote download function, and real-time display progress bar effect.

So, you need to read the size information of the remote file beforehand, and then do the parameters of the live download progress bar.

function functions as follows, the call is simple, getfilesize ("Http://www.alixixi.com/download/xml.rar"), you can get the size of the remote file.

The following is a reference fragment:
<?php
function GetFileSize ($url) {
$url = Parse_url ($url);
if ($fp = @fsockopen ($url [' Host '],empty ($url [' Port ']): $url [' Port '], $error)) {
Fputs ($fp, "get". Empty ($url [' path '])? ' /': $url [' path ']]. " Http/1.1\r\n ");
Fputs ($FP, "Host: $url [host]\r\n\r\n");
while (!feof ($fp)) {
$tmp = fgets ($FP);
if (Trim ($tmp) = = ") {
Break
}else if (Preg_match ('/content-length: (. *)/si ', $tmp, $arr)) {
return Trim ($arr [1]);
}
}
return null;
}else{
return null;
}
}
Echo getfilesize ("Http://www.alixixi.com/download/xml.rar")
?>

This function obtains the remote file size very well, regardless of the size of the remote file, the acquisition speed is completely unaffected.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.