2

我有一个下载脚本如下,在 Joomla 框架中运行:

if(headers_sent()) die('Headers Sent');

if (function_exists('finfo_file')) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $type = finfo_file($finfo, $dir.$filename);
    finfo_close($finfo);
} else {
    $type = mime_content_type($dir.$filename);
}
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off');  }

jimport( 'joomla.filesystem.file' );
$ext = strtoupper(JFile::getExt($filename));
$no_ext = JFile::stripExt($filename);

header('Content-Transfer-Encoding: Binary'); //need this to recognise in firefox that it is a PDF and not html doc
header('Content-length: '.filesize($dir.$filename));
header('Content-Type: application/octet-stream'); //works on browsers, but still fails on android (rather than unsuccessful) with 'redirect error'
header('Content-Disposition: attachment; filename="' . $no_ext.'.'.$ext.'"'); 
readfile($dir.$filename);
exit; //need to exit otherwise ff thinks this is a html document. 

该脚本在所有桌面浏览器和 Apple 产品上都可以正常工作,但在 Android 上几乎立即尝试失败。给出的错误是带有“.bin”文件的“重定向错误”,但使用稍微不同的代码我得到“无效的 URL”。

但是,如果我将它从我的资产存储库模型复制到 .php 页面(导入 Joomla 框架),并通过 Android 访问它,它会完美运行。通过模型下载时的 URL 是 www.domain.com/resources/download/40-alias-of-file 和静态 PHP 页面:www.domain.com/download.php?id=40

通过模型下载时,Android 上显示的文件名显示为 40-alias-of-file,但 Android 浏览器会接收 PHP 页面上给出的实际文件名。它是完全相同的代码,唯一的区别是 URL 和通过控制器/模型的路由。我尝试将 .pdf 附加到 URL 但没有运气:www.domain.com/resources/download/40-alias-of-file.pdf - 它为文件提供了一个 PDF 图标,但仍然不成功。

有人有想法么?

非常感谢!

4

1 回答 1

0

该文件以前/最近是否被修改过?您是否尝试过使用 fpassthru()?

if(headers_sent()) die('Headers Sent');

if (function_exists('finfo_file')) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $type = finfo_file($finfo, $dir.$filename);
    finfo_close($finfo);
} else {
    $type = mime_content_type($dir.$filename);
}
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off');  }

jimport( 'joomla.filesystem.file' );
$ext = strtoupper(JFile::getExt($filename));
$no_ext = JFile::stripExt($filename);
$fd = fopen($dir.$filename, "rb");

header('Pragma:  '); //I am surprised it worked with IE without this
header('Content-Transfer-Encoding: Binary'); //Try also without this header
header('Content-Length: '.filesize($dir.$filename));
header('Content-Type: '.$type); 
header('Content-Disposition: attachment; filename="' . $no_ext.'.'.$ext.'"'); 

if ($fd) {     
   fpassthru($fd);         
   fclose($fd);              
   exit();         
}
于 2013-03-04T08:28:41.697 回答