2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHPで他URLのファイルを転送してダウンロードさせる

Posted at

やっていることはトンネル。

function forwardBin($url) {

    // ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する
    header('X-Content-Type-Options: nosniff');

    // ダウンロードするべきファイル情報を先に取得して、その内容をそのまま設定
    $filename = basename($url);
    $headers = get_headers($url, 1);
    header('Content-Type: ').$headers['Content-Type'];
    header('Content-Length: '.$headers['Content-Length']);
    header("Content-Disposition: attachment; filename=${filename}");

    // readfile()の前に出力バッファリングを無効化する
    while (ob_get_level()) {
        ob_end_clean();
    }
    // ダウンロード
    readfile($url);

    exit;
}

【PHP】正しいダウンロード処理の書き方 を参考にしました。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?