3

how can I make sure that the file exists on the server and find out its size on the URL without first downloading the file

$url = 'http://site.zz/file.jpg';
file_exists($url); //always is false
filesize($url); //not working

Help eny one worked exemple pls

Evgeniy Chorniy
  • 141
  • 1
  • 1
  • 11

3 Answers3

5

The function file_exists() only works on file that exists on the server locally.

Similarly; filesize() function returns the size of the file that exists on the server locally.

If you are trying to load the size of a file for a given url, you can try this approach:

function get_remote_file_info($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    $data = curl_exec($ch);
    $fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    $httpResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return [
        'fileExists' => (int) $httpResponseCode == 200,
        'fileSize' => (int) $fileSize
    ];
}

Usage:

$url = 'http://site.zz/file.jpg';
$result = get_remote_file_info($url);
var_dump($result);

Example output:

array(2) {
  ["fileExists"]=>
  bool(true)
  ["fileSize"]=>
  int(12345)
}
Latheesan
  • 23,247
  • 32
  • 107
  • 201
5

Without any libraries and file openning

$data = get_headers($url, true);
$size = isset($data['Content-Length'])?(int) $data['Content-Length']:0;   

Open remote files:

function fsize($path) { 
      $fp = fopen($path,"r"); 
      $inf = stream_get_meta_data($fp); 
      fclose($fp); 
      foreach($inf["wrapper_data"] as $v) { 
        if (stristr($v, "content-length")) { 
          $v = explode(":", $v); 
          return trim($v[1]); 
        } 
      } 
      return 0;
    } 

Usage:

$file = "https://zzz.org/file.jpg"; 
$inbytes = fsize($filesize);

Use sockets:

function getRemoteFileSize($url){ 
   $parse = parse_url($url); 
   $host = $parse['host']; 
   $fp = @fsockopen ($host, 80, $errno, $errstr, 20); 
   if(!$fp){  
     $ret = 0;  
   }else{ 
     $host = $parse['host']; 
     fputs($fp, "HEAD ".$url." HTTP/1.1\r\n"); 
     fputs($fp, "HOST: ".$host."\r\n");  
     fputs($fp, "Connection: close\r\n\r\n"); 
     $headers = ""; 
     while (!feof($fp)){  
       $headers .= fgets ($fp, 128);  
     } 
     fclose ($fp); 
     $headers = strtolower($headers); 
     $array = preg_split("|[\s,]+|",$headers); 
     $key = array_search('content-length:',$array); 
     $ret = $array[$key+1]; 
   }   
   if($array[1]==200) return $ret; 
   else return -1*$array[1]; 
 } 
Evgeniy Chorniy
  • 141
  • 1
  • 1
  • 11
  • Seems a bit overkill if you ask me, you can achieve what you are after simply by analysing the http headers, see my updated answer. – Latheesan Sep 17 '18 at 13:55
  • best way to get remote file size is: $data = get_headers($url, true); $size = isset($data['Content-Length'])?(int) $data['Content-Length']:0; – Evgeniy Chorniy Sep 17 '18 at 13:56
  • agreed, that's exactly what I am doing, using curl - https://curl.haxx.se/libcurl/c/CURLINFO_CONTENT_LENGTH_DOWNLOAD.html – Latheesan Sep 17 '18 at 14:19
-3

You can't access to filesize of a distant file. You have to check with your local filepath.

slig36
  • 187
  • 3
  • 13