大きなファイルをダウンロードするためのPHPスクリプトを書く必要があります。私がここに報告しているファイルは、ほとんどの場合、大丈夫です。しかし、クライアントの接続が遅い場合は、ダウンロードの途中で要求が終了し(ステータスコード200)、必ずしも同じ時点ではなく、同時にではありません。私はいくつかのphp.ini変数を上書きしようとしましたが(最初の文を見てください)、問題は残っています。 私はそれが適切かどうかわかりませんが、私のホスティングサーバーはSiteGroundです。単純な静的ファイル要求の場合、ダウンロードは低速接続でも正常に動作します。大きなファイルをダウンロードする
私はForced downloading large file with phpを見つけましたが、私はマリオの答えを理解しませんでした。私はウェブプログラミングに新しいです。
ここに私のコードです。
<?php
ini_set('memory_limit','16M');
ini_set('post_max_size', '30M');
set_time_limit(0);
include ('../private/database_connection.php');
$downloadFolder = '../download/';
$fileName = $_POST['file'];
$filePath = $downloadFolder . $fileName;
if($fileName == NULL)
{
exit;
}
ob_start();
session_start();
if(!isset($_SESSION['Username']))
{
// or redirect to login (remembering this download request)
$_SESSION['previousPage'] = 'download.php?file=' . $fileName;
header("Location: login.php");
exit;
}
if (file_exists($filePath))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
//header('Content-Disposition: attachment; filename='.$fileName);
header("Content-Disposition: attachment; filename=\"$fileName\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
//header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
ob_clean();
flush();
// download
// 1
// readfile($filePath);
// 2
$file = @fopen($filePath,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
exit;
}
else
{
header('HTTP/1.1 404 File not found');
exit;
}
?>
http://stackoverflow.com/questions/597159/sending-large-files-reliably-in-phpを見ると、 'X-SENDFILE'ヘッダーに関する回答が1つあります。ただし、ホストがこのヘッダーをサポートしているかどうかを確認してください。 – Joost
私はそれが関連しているとは思わないが、私はASIHTTPRequestライブラリを使ってリクエストを行っています。私はASIFormDataRequestを構築し、downloadDestinationPathを設定します。 – dnaxxx
x-sendfileは、レスポンスが即時に到着し、ステータスコード200でファイルがないためサポートされていないようです。私はそれを可能にすることができるかどうかSitegroundに尋ねるつもりです。 – dnaxxx