Media Views は、バージョン 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 が必要です。上記のクックブックのリンクも参照してください。
古いバージョンの 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);
}