ファイルダウンロードのためのリンクをクリックするとエラーになります。
エラーになったあと戻ってファイルダウンロードのためのリンクをクリックするとダウンロードできたりできなかったりします。
エラーの場合error.logでは次の文言が出力されます。
「
Error: [NotFoundException] The requested file /Applications/MAMP/htdocs/cakephp/app/webroot/files/questions/ファイル名 was not found or not readable
Request URL: /questions_numbers/download/ファイル名
...」
ファイルはその場所にあります。
実際にダウンロードできることもあるので。
コードは次のとおりです
【Controller】
public function download($name){ $this->autoRender = false; $pass = APP . "webroot/files/questions/".$name; $this->response->file($pass,array('download' => true)); return $this->response;
}
【View】
<?php echo $this->Html->link($file_data['title'],array('controller' => 'コントローラー名','action' => 'download',$file_data['title']));?>
なお、次のようにControllerを書き換えて「not found or not readable」について検証してもTRUEを返します。
public
1 2 $this->autoRender = false; 3 4 $pass = APP . "webroot/files/questions/".$name; 5 6 if (is_readable($pass)){ 7 echo 'good'; 8 9 } else { 10 echo 'bad'; 11 } 12 13 if (file_exists($pass)){ 14 echo 'good2'; 15 16 } else { 17 echo 'bad2'; 18 } 19 20 21 } 22コード