-1

我正在尝试从站点下载一些文件,但一直遇到问题。首先它正在下载一个空文件,我认为这是因为内容类型,所以有人建议我现在正在做的 application/octet-stream。然后我在实际查找文件时遇到了很多问题。在几个测试用例之后,这是空白问题,然后我对其进行了修改(放入“”),现在我再次下载一个空文件。

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

<?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
4

2 回答 2

2

你从来没有真正输出文件,例如你失踪了

readfile($downloadFilename);

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

$_GET['file'] = '../../../../../../../../../etc/passwd';
于 2013-07-25T15:07:21.420 回答
1

这里的错字:

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

应该:

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

试试看

于 2013-07-25T15:06:46.560 回答