我的文件(126 MB大小,.exe)給我提出了問題。Laravel爲什麼不能輕鬆下載大文件?
我正在使用標準的laravel下載方法。
我試圖增加內存,但它仍然說我已經用完內存,或者我下載了一個0 KB大小的文件。
該文檔沒有提及任何有關大文件大小的內容。
我的代碼是
ini_set("memory_limit","-1"); // Trying to see if this works
return Response::download($full_path);
什麼我做錯了嗎?
- 編輯 -
你打算在菲爾火花評論,這是我和它的工作原理。 這是Phill's加上一些來自php.net的組合。 不確定是否有遺漏的東西?
public static function big_download($path, $name = null, array $headers = array())
{
if (is_null($name)) $name = basename($path);
// Prepare the headers
$headers = array_merge(array(
'Content-Description' => 'File Transfer',
'Content-Type' => File::mime(File::extension($path)),
'Content-Transfer-Encoding' => 'binary',
'Expires' => 0,
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'public',
'Content-Length' => File::size($path),
), $headers);
$response = new Response('', 200, $headers);
$response->header('Content-Disposition', $response->disposition($name));
// If there's a session we should save it now
if (Config::get('session.driver') !== '')
{
Session::save();
}
// Below is from http://uk1.php.net/manual/en/function.fpassthru.php comments
session_write_close();
ob_end_clean();
$response->send_headers();
if ($file = fopen($path, 'rb')) {
while(!feof($file) and (connection_status()==0)) {
print(fread($file, 1024*8));
flush();
}
fclose($file);
}
// Finish off, like Laravel would
Event::fire('laravel.done', array($response));
$response->foundation->finish();
exit;
}
它幾乎奏效,我一直使用readfile和fpassthru()獲取文件未找到erros。但是我查看了鏈接,並將其與您的鏈接合併在一起,以實現一些有效的功能。雖然我不確定它有多正確?我編輯了我的問題以顯示最新消息。 – Lango 2013-04-15 00:06:07
嗨@Lango,我看不出您的解決方案有任何問題。我已經更新了我的答案,包括fread + print方法和'ob_get_clean()'。我不明白爲什麼fpassthru在fread +打印時無法工作,因爲它們使用相同的文件指針 - 但是如果它適用於您,然後隨它一起滾動! – 2013-04-15 08:51:26
謝謝Phill!現在都在工作。 – Lango 2013-04-16 03:07:21