下载文件PHP时出错

时间:2014-05-14 04:34:01

标签: php download

我有一个链接显示要下载的文件名。当用户点击它时,需要下载。文件下载但只包含0 KB。在控制台显示

  

资源被解释为文档但使用MIME类型application / force-download传输:" ../ download.php?file = filename"

我的代码是这样的:

<a href="download.php?file=user_uploads/'.$_path['uploads'].
'logo_images/'.$row['FileName'].'" title="Click to download">'.$row['FileName'].'</a>

download.php是这样的:

<?php       
$path   =   str_replace('/download.php?file=','',$_SERVER['REQUEST_URI']);  
header("Content-Description: File Transfer");
header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=\"" . basename($path . $uri[1]) . "\"" );   
@readfile($path);  
?> 

提前谢谢。我也检查了文件的路径。

2 个答案:

答案 0 :(得分:1)

<a href="yourpath_to_download.php?file=file.txt" title="Click to download">Click</a>

的download.php

<?php       
$path   = 'yourpath'.$_GET['file'];  
header("Content-Description: File Transfer");
header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=".$_GET['file'] );   
@readfile($path);  
?> 

file.txt - 使用您的文件名更改

答案 1 :(得分:0)

我在下载文件时遇到了类似的问题。我将此代码用于download.php:

<?php

$path = $_REQUEST['path'];

#setting headers
   header('Content-Description: File Transfer');
   header('Cache-Control: public');
   header('Content-Type: application/zip');
   header("Content-Transfer-Encoding: binary");
   header('Content-Disposition: attachment; filename='. basename($path));
   header('Content-Length: '.filesize($path));
   ob_clean(); #THIS!
   flush();
   readfile($path);

exit;
?>

我的链接是:

<a href="file.php?path='.$encodedPath.'" name="download" class="acction" ">Download Pack</a>

希望它有所帮助。