下载的文件为空或无法找到它开始下载

时间:2013-07-25 15:03:09

标签: php html download

我正在尝试从网站下载一些文件,但一直遇到问题。首先它是下载一个空文件,我认为是因为内容类型,有人在SO上建议我正在做的应用程序/八位字节流。然后,我遇到了很多问题,实际上找到了文件。在几个测试用例后,这是空白问题,然后我修改了(放入“”),我现在再次下载一个空文件。

基本代码,但希望有人可以提供帮助:

<?php
$filename = $_GET['filename'];
$dir = "training/trainingDocuments/";
$downloadFilename = $dir.$filename;
//ftp://site.com///training/trainingDocuments/8%20-%20SUBSEA%20QUESTIONS__51f034ab37a8e.xls
//$downloadFilename = preg_replace('/\s/', '%', $downloadFilename);

//8 - SUBSEA QUESTIONS__51f034ab37a8e.xls
//if the filename exists
if(is_file($downloadFilename)){

//send the headers
header('Pragma: public'); //FIX IE6 Content-Disposition
header('Content-DEscription: File Transfer');
header('Content-Transder-Encoding: binary');
header(sprintf('Content-Length: %u', filesize($downloadFilename)));
header('Content-type: application/octet-stream');
header("Content-Disposition: attachment; filename=".$downloadFilename."");

}else{
//file doesn't exist
echo "File no exist\n\n";
echo $downloadFilename;
}//end if file exists

2 个答案:

答案 0 :(得分:2)

你永远不会实际输出文件,例如你错过了

readfile($downloadFilename);

同样请注意,您的代码允许恶意用户在您的服务器上下载他们知道路径的 ANY 文件。考虑

$_GET['file'] = '../../../../../../../../../etc/passwd';

答案 1 :(得分:1)

错字:

header('Content-Transder-Encoding: binary');

应该是:

header('Content-Transfer-Encoding: binary');

试试