php获取远程文件的http状态及大小

后端开发PHP

PHP获取远程文件的http状态

$img = "http://youimageUrl";

$ch = curl_init($img);

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode >= 400 -> not found, $retcode = 200, found.
curl_close($ch);

echo $retcode;

获取文件大小

/**
 * 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, "" );

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

Read Comments

  • AI lover1 year ago0

    您好,https://towait.com/blog/tensorflow-cnn-flowers/ 这个花的分类我训练后的loss 和acc值都是nan值,请问一下咋回事啊- -代码都是您写好的

    • Julian1 year ago0

      首先非常抱歉,关于TF我也是新手来着,但是站点内的代码都是我经过测试运行通过的,我用的都是GPU版的TF,你NAN的问题我在Github找到一些答案,使用CPU版可能会出现这个问题,但是在GPU版本下OK:
      1.The same problem on Tesla M2090. I tried consume_less gpu and cpu. GRU is working ok.
      2.On CPU I am getting nans too, but after more epochs than on GPU.
      希望能帮到你!

Post Comment