Codeigniter force_download () for excel file loading damaged file

Below is the code that I use to force download some files on my server in codeigniter. The downloaded file is damaged and MS Excel cannot open it. I do not see any problems with this code. I downloaded the files using ftp from the server and checked and they just opened. I do not know where I was wrong.

$this->load->helper('download'); $path = base_url('reference/filename.xlsx'); $data = file_get_contents($path); // Read the file contents $name = 'filename.xlsx'; force_download($name, $data); 
+4
source share
4 answers

I had the same problem and CI does not support xlsx by default. You will need to find lib for this. In any case, you can download csv if you want.

0
source

I realized that loading Excel xlsx files is very convenient if you use force_download () inside the model before loading any html content. I use force_download inside my navigation model before loading any views.

0
source

Flush the output buffer with the ob_clean () function just before you get your data.

Your code should now look like this.

 $this->load->helper('download'); $path = base_url('reference/filename.xlsx'); ob_clean(); $data = file_get_contents($path); // Read the file contents $name = 'filename.xlsx'; force_download($name, $data); 
0
source

Your answer is correct. but the path is wrong. but does not display an error message. comment and run. then you can see the path error message

 //force_download($name, $data); 

and fix the path to fix and delete the comment and execute again, then open the downloaded file.

0
source

Source: https://habr.com/ru/post/1492097/


All Articles