萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

.php 後綴的種子文件怎麼打開?Content-Type 與副檔名關聯修正

現象分析

下載的種子文件顯示為 xxx.php 而非 xxx.torrent,通常是因為:

  1. 服務器端腳本 download.php 輸出種子內容,但未正確設置 Content-Disposition 頭
  2. 瀏覽器根據 URL 路徑 (/download.php?id=123) 推斷文件名為 download.php
  3. 實際內容是標準 BitTorrent 格式 (Bencode 編碼)

解決方案

方法一:手動重命名 (最簡單)

下載後直接重命名:mv download.php movie.torrent,然後用迅雷、qBittorrent、uTorrent、Transmission 等 BT 客戶端打開。

方法二:服務端修正響應頭 (根治)

修改下載腳本 download.php:

<?php
$torrentPath = '/path/to/real.torrent'; // 或從數據庫讀取二進制內容
$filename = 'movie.torrent'; // 用戶看到的文件名

if (!file_exists($torrentPath)) {
    http_response_code(404);
    exit('文件不存在');
}

// 關鍵頭部
header('Content-Type: application/x-bittorrent'); // 標準 MIME
header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"');
header('Content-Length: ' . filesize($torrentPath));
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: public');

// 輸出文件
readfile($torrentPath); // 比 file_get_contents + echo 更省記憶體
exit;

方法三:Nginx/Apache 直接服務靜態 .torrent 文件 (最高性能)

# Nginx
location /torrents/ {
    alias /var/www/torrents/;
    add_header Content-Type application/x-bittorrent;
    add_header Content-Disposition 'attachment; filename="$1"';
    # 或者讓 PHP 生成簽名 URL,Nginx X-Accel-Redirect 內部重定向
}

# Apache .htaccess
AddType application/x-bittorrent .torrent
Header set Content-Disposition "attachment"

PHP 生成種子文件 (補充)

<?php
// 簡單生成 .torrent (單文件模式)
function createTorrent(string $filePath, string $announceUrl, string $outputPath): bool
{
    $fileName = basename($filePath);
    $fileSize = filesize($filePath);
    $pieceLength = 256 * 1024; // 256KB 每片
    
    $pieces = '';
    $fp = fopen($filePath, 'rb');
    while (!feof($fp)) {
        $piece = fread($fp, $pieceLength);
        $pieces .= pack('H*', sha1($piece));
    }
    fclose($fp);
    
    $dict = [
        'announce' => $announceUrl,
        'info' => [
            'name' => $fileName,
            'piece length' => $pieceLength,
            'pieces' => $pieces,
            'length' => $fileSize,
        ],
        'creation date' => time(),
        'created by' => 'PHP Torrent Creator 1.0',
    ];
    
    $encoded = bencode($dict); // 需實現 Bencode 編碼
    return file_put_contents($outputPath, $encoded) !== false;
}

// Bencode 簡易實現
function bencode($data): string
{
    if (is_int($data)) return 'i' . $data . 'e';
    if (is_string($data)) return strlen($data) . ':' . $data;
    if (is_array($data)) {
        $isList = array_keys($data) === range(0, count($data) - 1);
        $res = $isList ? 'l' : 'd';
        if ($isList) {
            foreach ($data as $v) $res .= bencode($v);
        } else {
            ksort($data);
            foreach ($data as $k => $v) {
                $res .= bencode($k) . bencode($v);
            }
        }
        return $res . 'e';
    }
    return '';
}

常見 MIME 類型對照

總結

  • 用戶端:直接重命名 .php → .torrent 即可用 BT 軟件打開
  • 開發者:務必設置 Content-Disposition: attachment; filename="xxx.torrent" 和正確 Content-Type
  • 大流量站點:靜態化 .torrent 文件,Nginx 直接服務,或簽名 URL + X-Accel-Redirect
copyright © 萬盛學電腦網 all rights reserved
副檔名Content-Type用途
.torrentapplication/x-bittorrentBitTorrent 種子
.magnetapplication/x-magnet磁力鏈接 (文本)
.phpapplication/x-httpd-phpPHP 腳本 (服務端解析)
.htmltext/html網頁