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 として表示されますが、PHP ページで指定された実際のファイル名は Android ブラウザーによって受信されます。これはまったく同じコードです。唯一の違いは、URL とコントローラー/モデルを介したルーティングです。URL に .pdf を追加しようとしましたが、うまくいきませんでした: 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 に答える