1

i am using below code to check the size of remote image and based on output it shows ok or not ok . but in one case where pinterest.com is there as url it gives wrong output.

for $op9 if link is other than pinterest it gives correct output but in pinterest it gives wrong output and one more thing i have noticed however the link is ending with .jpg in pinterest but it will not display any image instead it will ask for facebook login any idea how can i solve this issue

CODE:

<?php

$op9 = 'http://pinterest.com/pin/create/button/?url=http://www.glamsham.com/download/wallpaper/22705/emraan-hashmi-wallpapers/47567.htm&media=http://media.glamsham.com/download/wallpaper/celebrities/images/e/emraan-hashmi-wallpaper-40-12x9.jpg';

//$op9 = 'http://photos1.meetupstatic.com/photos/member/9/3/6/e/member_87697742.jpeg';

$head = array_change_key_case(get_headers($op9, TRUE));
$filesize = $head['content-length'];

if ($filesize >= 31000) {
    echo 'ok';
}
?>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Priya
  • 165
  • 1
  • 12
  • Uhm .. why not use the file itself? Why do you need pinterest ? Like: `http://media.glamsham.com/download/wallpaper/celebrities/images/e/emraan-hashmi-wallpaper-40-12x9.jpg`, you can get it like: `parse_str($op9, $str); $img = $str['media'];` – Mihai Iorga Dec 25 '13 at 11:04
  • @MihaiIorga i dont have control on link so i have to fetch it but i want to ignore images which are not really image – Priya Dec 25 '13 at 11:06

1 Answers1

2

In first case:

$op9 = 'http://pinterest.com/pin/create/button/?url=http://www.glamsham.com/download/wallpaper/22705/emraan-hashmi-wallpapers/47567.htm&media=http://media.glamsham.com/download/wallpaper/celebrities/images/e/emraan-hashmi-wallpaper-40-12x9.jpg';

$filesize = $head['content-length'] will be an array

array (size=5)
  0 => string '178' (length=3)
  1 => string '0' (length=1)
  2 => string '0' (length=1)
  3 => string '0' (length=1)
  4 => string '0' (length=1)

and you compare it with ">=31000" and you will have "ok"

In second case:

$op9 = 'http://photos1.meetupstatic.com/photos/member/9/3/6/e/member_87697742.jpeg';

$filesize = $head['content-length'] will be a string '57412' (length=5)

That's why if you compare $filesize with ">=31000" you will not have "ok"

Eited. Use strlen to get size:

$op9 = 'http://pinterest.com/pin/create/button/?url=http://www.glamsham.com/download/wallpaper/22705/emraan-hashmi-wallpapers/47567.htm&media=http://media.glamsham.com/download/wallpaper/celebrities/images/e/emraan-hashmi-wallpaper-40-12x9.jpg';

//$op9 = 'http://photos1.meetupstatic.com/photos/member/9/3/6/e/member_87697742.jpeg';

$filesize = strlen(file_get_contents($op9));
echo $filesize;  //here you will see the size
if ($filesize >= 31000) {
    echo 'ok';
}

If you does not want to download the file use next:

Found something about this here and here:

Here's the best way (that I've found) to get the size of a remote file. Note that HEAD requests don't get the actual body of the request, they just retrieve the headers. So making a HEAD request to a resource that is 100MB will take the same amount of time as a HEAD request to a resource that is 1KB.

<?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, get_user_agent_string() );

  $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;
}
?>

Usage:

$file_size = curl_get_file_size( "https://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file" );
Community
  • 1
  • 1
sergio
  • 5,210
  • 7
  • 24
  • 46