filesize content-length的问题

时间:2013-12-25 10:58:49

标签: php

我使用下面的代码来检查远程图像的大小,并根据输出显示确定或不正常。但在一个例子中,pinterest.com就像url一样,它输出错误。

对于$ op9如果链接不是pinterest它给出了正确的输出但是在pinterest它给出了错误的输出并且我注意到了另外一件事但是链接以pinterest中的.jpg结尾但它不会显示任何图像而是它将会请求facebook登录任何想法如何解决这个问题

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

1 个答案:

答案 0 :(得分:2)

在第一种情况下:

$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'] 将是一个数组

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)

并将其与“&gt; = 31000”进行比较,您将获得“确定”

在第二种情况下:

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

$ filesize = $ head ['content-length'] 将是一个字符串'57412'(长度= 5)

这就是为什么如果你将$ filesize与“&gt; = 31000”进行比较,你将没有“ok”

<强> Eited。使用strlen来获取大小:

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

如果您不想下载文件,请使用下一个:

找到关于此herehere

的内容
  

这是获得遥控器大小的最佳方式(我发现)   文件。请注意,HEAD请求无法获取请求的实际主体,   他们只是检索标题。因此向资源发出HEAD请求   100MB将花费与HEAD请求相同的时间   资源是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;
}
?>

用法:

$file_size = curl_get_file_size( "https://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file" );