1

Till now i used the following function to get filesize of files from url.It works perfectly fine if the url is http but fails when its https.Can anyone update it to work with https.

<?php
/**
 * Returns the size of a file without downloading it, or -1 if the file
 * size could not be determined.
 *
 * @param $url - The location of the remote file to download. Cannot
 * be null or empty.
 *
 * @return The size of the file referenced by $url, or -1 if the size
 * could not be determined.
 */
function curl_get_file_size( $url ) {
  // Assume failure.
  $result = -1;

  $curl = curl_init( $url );

  // Issue a HEAD request and follow any redirects.
  curl_setopt( $curl, CURLOPT_NOBODY, true );
  curl_setopt( $curl, CURLOPT_HEADER, true );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );

  $data = curl_exec( $curl );
  curl_close( $curl );

  if( $data ) {
    $content_length = "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 ) ) {
      $content_length = (int)$matches[1];
    }

    // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    if( $status == 200 || ($status > 300 && $status <= 308) ) {
      $result = $content_length;
    }
  }

  return $result;
}
?>

This is not my code btw and the full credit goes to NebuSoft Source: PHP: Remote file size without downloading file

Regards

Community
  • 1
  • 1
user2650277
  • 6,289
  • 17
  • 63
  • 132

5 Answers5

1

You need to add the below cURL parameter to your existing set.

curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0);
  • Adding this will stop cURL from verifying the peer's certificate.
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

In this, you have not used curl host verify'r, you have to set this for the communication verify purpose.

Just add this to your parameters

curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false);
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
0

Try strlen(file_get_contents($url)); this will get the length of content through this

keshu_vats
  • 452
  • 6
  • 45
0

use this one:

<?php
$remoteFile = $url;//remote url
$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;
?>

Refer: http://in3.php.net/filesize

Mani
  • 888
  • 6
  • 19
0
function remotefileSize($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_exec($ch);
    $filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);
    if ($filesize) return $filesize;
}
echo remotefileSize('http://www.google.com/images/srpr/logo4w.png');
//Output
//19978 bytes
debasish
  • 735
  • 1
  • 9
  • 14