php filesize not working for URL (CDN URL)

守給你的承諾、 提交于 2019-12-11 02:44:00

问题


I am trying to get the fileSize of a file using this answer

this is my php code

<?php
echo filesize('LICENSE.txt');
echo filesize('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg');
?>

Above code only works for local files but when I try to get filesize of CDN URL, I get error

Warning: filesize() [function.filesize]: stat failed for http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg in test_db_operation() (line 80 of myphp.php).

Based on the filesize php manual here

Description ¶

int filesize ( string $filename )
Gets the size for the given file.

Parameters ¶

**filename
Path to the file.

**Return Values ¶

Returns the size of the file in bytes, or FALSE (and generates an error of level E_WARNING) in case of an error.

Does filesize not work with URL or I am doing something wrong here. Please help !!! Thanks


回答1:


Use this code to find the file size...

<?php
$remoteFile = 'http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
  $status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>

Result:

HTTP Status: 302
Content-Length: 8808759

SOURCE : http://php.net/manual/en/function.filesize.php#92462




回答2:


there is some way to get remote file size

1:get_headers

print_r(get_headers('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg',true)); 

2:curl

function remote_filesize($uri,$user='',$pw='')    
{    
     // start output buffering    
     ob_start();    
     // initialize curl with given uri    
    $ch = curl_init($uri);    
    // make sure we get the header    
    curl_setopt($ch, CURLOPT_HEADER, 1);    
    // make it a http HEAD request    
    curl_setopt($ch, CURLOPT_NOBODY, 1);    
    // if auth is needed, do it here    
   if (!emptyempty($user) && !emptyempty($pw))    
  {    
      $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));    
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    
  }    
   $okay = curl_exec($ch);    
   curl_close($ch);    
   // get the output buffer    
   $head = ob_get_contents();    
   // clean the output buffer and return to previous    
   // buffer settings    
   ob_end_clean();    

   echo '<br>head-->'.$head.'<----end <br>';    

  // gets you the numeric value from the Content-Length    
  // field in the http header    
  $regex = '/Content-Length:\s([0-9].+?)\s/';    
  $count = preg_match($regex, $head, $matches);    

  // if there was a Content-Length field, its value    
  // will now be in $matches[1]    
  if (isset($matches[1]))    
  {    
      $size = $matches[1];    
  }    
  else    
  {    
      $size = 'unknown';    
  }    
  //$last=round($size/(1024*1024),3);    
  //return $last.' MB';    
  return $size;    
}   

3:socket

function getFileSize($url)    
 {    
    $url = parse_url($url);    
    if($fp = @fsockopen($url['host'],emptyempty($url['port'])?80:$url['port'],$error))    
   {    
       fputs($fp,"GET ".(emptyempty($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;    
          }    
           elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))    
          {    
              return trim($arr[1]);    
          }    
      }    
      return null;    
   }    
    else    
    {    
        return null;    
    }    
  }  

4:file_get_contents

$fCont = file_get_contents('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg');    
 echo strlen($fCont)/1024;  



回答3:


You can use,

getimagesize();

it will give you the CDN image file size.

$src = "CDN_URL";
list($width, $height) = getimagesize($src);
echo "ImageUrl: ". $src . "<br/>";
echo "width: " . $width . "<br />";
echo "height: " .  $height;

If it's user uploading file you can use,

$_FILES['file']['size']

from server side to get the file size.

With jQuery,

<input type="file" id="myFile" />
$('#myFile').bind('change', function() {
  //this.files[0].size gets the size of your file.
  alert(this.files[0].size);
});


来源:https://stackoverflow.com/questions/25909565/php-filesize-not-working-for-url-cdn-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!