PHP:从服务器下载文件
我已经尝试了我找到的各种代码,但没有任何东西对我有效 当我使用readfile()或fopen()或类似的东西时:PHP:从服务器下载文件,php,download,Php,Download,我已经尝试了我找到的各种代码,但没有任何东西对我有效 当我使用readfile()或fopen()或类似的东西时: function makeDownload($file, $dir, $type) { header("Content-Type: $type"); header("Content-Disposition: attachment; filename=\"$file\""); readfile($dir.$file); } 。。。已开始下载,但文件始终为空 以下是我最后尝试
function makeDownload($file, $dir, $type) {
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($dir.$file);
}
。。。已开始下载,但文件始终为空
以下是我最后尝试的代码:
$filename = "gandalf.jpg";
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo " filename NULL";
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = 'upload/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
echo "file exists";
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: image/png');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error message if file can't be opened
//readfile($path.$filename);
$file = @ fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
$filename=“gandalf.jpg”;
//定义错误消息
$err='抱歉,您请求的文件不可用。
';
如果(!$filename){
//如果变量$filename为NULL或false,则显示消息
回显“文件名为空”;
echo$err;
}否则{
//定义下载文件夹的路径并指定文件名
$path='upload/。$filename;
//检查文件是否存在且可读
如果(文件存在($path)&&is_可读($path)){
echo“文件存在”;
//获取文件大小并发送http头
$size=文件大小($path);
标题('Content-Type:image/png');
标题(“内容长度:”.$size);
标题('Content-Disposition:attachment;filename='。$filename);
标题(“内容传输编码:二进制”);
//以二进制只读模式打开文件
//如果无法打开文件,则显示错误消息
//readfile($path.$filename);
$file=@fopen($path,'rb');
如果($file){
//流式传输文件,完成后退出脚本
fpassthru($文件);
出口
}否则{
echo$err;
}
}否则{
echo$err;
}
}
这是我的代码的来源:
我希望你能帮助我:)
我使用谷歌浏览器
我需要你的帮助来完成这个小项目:
当您单击下拉列表中的文件时,会出现“herunterladen”(德语下载),我想从这里开始下载php代码
我只需要最基本的PHP下载功能。。。为什么w3schools上有一个上传示例而没有下载示例?哦
$fileSource= $_GET['fileSource']; //If you are passing the filename as a URL parameter
$fileSource= "sample.pdf" //If the filename is fixed in the current folder
if($fileSource) {
$filesize = filesize($fileSource);
$path_parts = pathinfo($fileSource);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-Disposition: attachment;
filename=\"".$path_parts["basename"]."\""); // use 'attachment' to
force a download
header("Content-type: application/pdf"); // add here more headers
for diff. extensions
break;
default:
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
if($filesize) {
header("Content-length: $filesize");
}
readfile($fileSource);
exit;
}说明您使用的浏览器以及如何使用下载功能非常重要。特别是Safari有一些严重的“安全特性”,例如,当涉及到通过javascript触发下载时。我花了一整天的时间在那个问题上琢磨虽然这很可能不是问题的原因,但它仍然可以让事情变得清楚代码>行