php - How to download a csv file in CakePHP -


I have a CSV file in my web site / file. Whenever I click on the link, I need to download the CSV file. I have written the following code and the file is also downloading but there is no content in the downloaded file. So please help download CSV file with all the content.

This is an IMI code:

  public function download sample () {$ name = "abc format.csv"; $ FILE_PATH = HTTP_ROOT "app \ webroot \ files" $ name .. Header ('content-description: file transfer'); Header ('content-type: application / force-download'); Header ("Content-Attention: Attachment; File Name = \" ".BassName ($ name)." \ ";"); Header ('content-transfer-encoding: binary'); Header ('expiration: 0'); Header ('cache-control: required-modified'); Header ('pragma: public'); Header ('content-length:'. Filesystems ($ name)); $ File = @fopen ($ file_path); Ob_clean (); Flush (); Readfile (.html_ROOT "app \ webroot \ files" $ name.); @fclose ($ file); Go out; }  

The problem is in the path (var $ file_path ) I guess

If is a folder inside files the app \ webroot \ then $ file_path will be variable

  $ file_path = Root. "App \\ webroot \\ files \\". $ Name; // \ 'file' // is also needed after the double order \ Coz order, try with single / / as follows: file_path = root The best way to use the functionality of "App / webroot / files / ./cake php" is $ file_path = WWW_ROOT. "Files" .DS. $ Name;  

and Also change the readfile and function parameters:

  readfile ($ file_path); header ('content-length:'. Filesystem ($ file_path)); @ as Steve's comment   ;  

Because the function automatically closes the file automatically.


Try it again

  $ name = "ABC% 20format.csv"; // is encoded as location% 20 in the URL of the url Is  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -