【发布时间】:2010-12-16 20:35:29
【问题描述】:
我有一个自动下载文件的脚本。 它可以完美地下载文件,但问题是 50% 或更多的时间,它会下载损坏的文件。
通常删除并再次下载会起作用,但并非总是如此。
我怎样才能让这个下载始终 100% 完美,不损坏? 文件大小因下载的文件而异。
<?php
// Automatically Start File Download
if (isset($_GET['filename'])):
$filename = $_GET['filename'];
$domain = "http://www.domain.com";
$filepath = "/addons/downloads/websites/";
//BUILD THE FILE INFORMATION
$file = $domain . $filepath . $filename;
// echo $filepath . $filename;
// echo $file;
//CREATE/OUTPUT THE HEADER
if (file_exists("/home/unrealde/public_html/ebook/domain.com/".$filepath . $filename)):
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
else:
$errorMsg = "<b>Download Error: File $filename Doesnt Exist!</b> <br />Please Contact <a href='mailto:support@domain.com'>support@domain.com</a>";
endif;
echo $errorMsg;
else:
// don't download any file
endif;
?>
【问题讨论】:
-
那么...如果它总是在 50% 的时间里损坏,难道它只有一半的时间损坏了吗?
-
某事怎么会“总是”并且有 50% 的时间?
-
只是为了引起争议 :-) 但说真的,我需要一些帮助
-
建议 1:理清你的缩进 - 它让阅读代码变得非常困难。建议 2:为您的
if()s 使用大括号{}符号而不是冒号:符号;再次,它会更容易阅读。 (它们只是建议,但当代码易于阅读时更容易提供帮助:-)) -
“50% 的时间,它每次都有效。”
标签: php file header download corrupt