メディアビューはバージョン2.3以降で非推奨です。代わりにSending filesを使用してください。
は、あなたのコントローラでは、この最小限の例をチェックアウト:
public function download($id) {
$path = $this->YourModel->aMagicFunctionThatReturnsThePathToYourFile($id);
$this->response->file($path, array(
'download' => true,
'name' => 'the name of the file as it should appear on the client\'s computer',
));
return $this->response;
}
$this->response->file
の最初のパラメータは、あなたのAPP
ディレクトリに対する相対パスです。したがって、$this->response->file('someFolder' . DS . 'someFile.zip')
を呼び出すと、ファイルAPP/someFolder/someFile.zip
がダウンロードされます。
「ファイルを送信する」には、少なくともCakePHPバージョン2.0が必要です。また、上記のCookbookのリンクを見てみることをお勧めします。
あなたはすでにあなたの質問で述べたように、あなたがメディアビューを使用する必要がありますCakePHPの古いバージョンを実行している場合。このコードを使用し、Media Views (Cookbook)を参照してください。
public function download($id) {
$this->viewClass = 'Media';
$path = $this->YourModel->aMagicFunctionThatReturnsThePathToYourFile($id);
// in this example $path should hold the filename but a trailing slash
$params = array(
'id' => 'someFile.zip',
'name' => 'the name of the file as it should appear on the client\'s computer',
'download' => true,
'extension' => 'zip',
'path' => $path
);
$this->set($params);
}
出典
2013-04-08 20:56:13
Jan
Apacheを使用している場合は、「ダウンロード」ディレクトリ内の '.htaccess'を追加することにより、ダウンロードを強制することができます
ここでは、古いバージョンの同じ方法です。いくつかの例をここに見てください:http://www.givegoodweb.com/post/30/forcing-a-download-with-apache-and-htaccess and http://www.timewasters-place.com/how-to-force -file-downloading-with-htaccess/PHPでダウンロードするファイルを提供する必要はありません – thaJeztah
APP_DIR/webroot/someFolder /に配置する方法は? – SaidbakR
@sємsємあなたはそうです(ファイルがウェブルートにないことに気付かなかった)。パブリックディレクトリに置くだけで自動的にはダウンロードされません(たとえば、ファイルがJPGの場合)。 'ダウンロード'ヘッダーを送信する.htaccessを追加すると、ブラウザ内でファイルを見るのではなく、ファイルを簡単にダウンロードできるようになります。 PHPを見ることは可能ですが(下記の答えを参照してください)、これは必ずしも大規模なファイルでは必ずしも理想的ではありません。 – thaJeztah