首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 透過 Socket 獲取網頁文件大小實作範例

     編輯:PHP 相關知識

核心概念

利用 fsockopen() 建立 TCP 連線至目標主機 80 埠,發送 HTTP/1.1 HEAD 或 GET 請求,解析回應標頭中的 Content-Length 即可得知檔案大小,無需下載完整內容。

完整程式碼

<?php
function getRemoteFileSize($url) {
    $parsed = parse_url($url);
    if (!$parsed || !isset($parsed['host'])) {
        return false;
    }

    $host = $parsed['host'];
    $path = isset($parsed['path']) ? $parsed['path'] : '/';
    if (isset($parsed['query'])) {
        $path .= '?' . $parsed['query'];
    }
    $port = isset($parsed['port']) ? $parsed['port'] : 80;

    $fp = @fsockopen($host, $port, $errno, $errstr, 10);
    if (!$fp) {
        return false;
    }

    // 發送 HEAD 請求即可,不需下載主體
    $request = "HEAD $path HTTP/1.1\r\n";
    $request .= "Host: $host\r\n";
    $request .= "Connection: Close\r\n\r\n";

    fwrite($fp, $request);

    $headers = '';
    while (!feof($fp)) {
        $line = fgets($fp, 1024);
        $headers .= $line;
        if ($line === "\r\n") break; // 標頭結束
    }
    fclose($fp);

    // 解析 Content-Length
    if (preg_match('/Content-Length:\s*(\d+)/i', $headers, $matches)) {
        return (int)$matches[1];
    }
    return false;
}

// 測試
$url = 'https://www.example.com/largefile.zip';
$size = getRemoteFileSize($url);
if ($size !== false) {
    echo "檔案大小: " . number_format($size) . " bytes (" . round($size/1024/1024, 2) . " MB)";
} else {
    echo "無法取得檔案大小";
}
?>

關鍵點說明

  • 使用 HEAD 方法:僅請求標頭,節省頻寬與時間。
  • 正則解析:/Content-Length:\s*(\d+)/i 不區分大小寫,兼容不同伺服器回應格式。
  • 錯誤處理:連線失敗、無 Content-Length(如動態頁面、Chunked 傳輸)皆回傳 false。

進階應用

  • 支援 HTTPS:將 fsockopen 改為 ssl:// 協定並連接 443 埠,或改用 stream_context_create 搭配 file_get_contents。
  • 大檔案斷點續傳:結合 Range 標頭實作下載進度條。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved