【问题标题】:Error in downloading file PHP下载文件PHP时出错
【发布时间】:2014-07-01 23:30:01
【问题描述】:

我有一个显示要下载的文件名的链接。当用户单击它时,需要下载它。文件已下载但它仅包含 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);  
?> 

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

【问题讨论】:

标签: php download


【解决方案1】:

试试

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

下载.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 - 更改为您的文件名

【讨论】:

  • 我之前用过 '$path = yourpath'.$_GET['file'];文件未下载。(例如:文件名 os jh&ju.txt,$path 变量将仅获得 jh
【解决方案2】:

我在下载文件时遇到了类似的问题。我将此代码用于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>

希望对你有帮助。

【讨论】:

  • 什么是 $packPath? @AppleBud
  • 抱歉,打错字了。它只是路径变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
相关资源
最近更新 更多