5

2 日以上前から解決しようとしている問題に遭遇しました。cakephp を使用して Web サイトを構築しましたが、すべて正常に動作していますが、 に保存されているファイルへのダウンロード リンクを実装しようとしたときに行き詰まりましたAPP_DIR/someFolder/someFile.zip

内のファイルへのダウンロード リンクを設定するにはどうすればよいsomeFolderですか? 私はしばしば「Media Views」に出くわし、それらを実装しようとしましたが、これまでのところ成功していません。

また、ファイルをダウンロード可能にする簡単な方法はありませんか?

4

2 に答える 2

17

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);
}
于 2013-04-08T20:56:13.957 に答える