cakephp - how to avoid .html extension being appended when downloading or opening my file -
when saving file in img/upload folder file saved correct file-extension.
however, when try download file, .htm file-extension appended.
how can avoid this? i've added code below;
view.ctp
<?php echo $this->form->label("resume:");?> <?php echo $this->form->input("resume",array("class"=>"input_boxstyle_select","label"=>"","type"=>"file","id"=>"file"));?> <a href="../download_resume/<?php echo $editemppros[0]['prospective_employee']['resume']?>" style="margin-left:140px;color:#0477ca;"> <?php echo $editemppros[0]['prospective_employee']['resume']?> </a>
inside controller:
public function download_resume($id=null) { $luser = $this->session->read('username'); $this->disablecache(); if (!$luser) { $this->redirect(array("action"=>"../")); } $path="../webroot/img/upload/$id"; header('content-disposition: attachment'); readfile($path); //print_r(readfile($path)); exit; }
handling file-downloads in cakephp 2.x
while other solutions may work, cakephp handles response via cakeresponse
object. chapter in manual describes how send (download) files; sending files
the response automatically attempt set right mime-type, based on file-extension
to output file (inside browser);
$this->response->file(webroot_dir . '/img/upload/' . $filename); //return reponse object prevent controller trying render view return $this->response;
to download file (and, optionally, specify custom filename)
to force downloading file , specify custom filename (if desired), use code. cakeresponse object automatically set right headers, manually specifying custom filename should not necessary
// force *downloading* file , specify custom filename (if desired) $this->response->file( webroot_dir . '/img/upload/' . $filename, array( 'download' => true, 'name' => 'custom-filename-for-downloading' ) ); return $this->response;
Comments
Post a Comment