0

I have uploaded files on server and I want to download on button click. But I did tried with below codes, it's working only on image files. Not on other extensions like pdf, xlsx, txt, docx and others. How should i do that to download all extensions.

Here is my View Code.

<a href="{base_url('CustomerInformDetailEditController/fileDownload/')}{$row->file_path}{$row->file_name}">Download</a>

Here is my controller Code.

function fileDownload(){
    $this->load->helper('download');

    $filename =  urldecode($this->uri->segment(5));
    $filepathName = file_get_contents(base_url(). $this->uri->segment(3) . "/" . $this->uri->segment(4) . "/" . $filename);
    echo $filepathName;
    force_download($filename, $filepathName);
}
Kawazoe Kazuke
  • 175
  • 6
  • 20

1 Answers1

1

English is not my native language.

I think, if your href link is correct and if you add the download attribut to your view like this, it works.

<a href="{base_url('CustomerInformDetailEditController/fileDownload/')}{$row->file_path}{$row->file_name}" download>Download</a>

See w3c documentation about download attribut.

If my issue doesn't work, look at the side of href.

If you have many documents to manage, i suggest to create a "document_model" for store documents informations in bdd and create a upload_document library for manipulate them.

If you read the documentation about it, you see the second parameters is data file contents not the filePathName but, you should rename your variable.

MaolmeoX
  • 46
  • 3
  • Yeah. I tried like u said. It's working on all format. Thanks alot brother. `Download` – Kawazoe Kazuke May 11 '17 at 01:53
  • Hello Friend. When i download the pdf file it's not download and it's only show directly in browser – Kawazoe Kazuke May 11 '17 at 02:02
  • The pdf file is download in temporary file on your desktop, you can save it on clicking save button on the top of your browser. If not, for solve your problem, look [here](http://stackoverflow.com/questions/9195304/how-to-use-content-disposition-for-force-a-file-to-download-to-the-hard-drive) or [here](http://stackoverflow.com/questions/6293893/how-to-force-files-to-open-in-browser-instead-of-download-pdf). – MaolmeoX May 11 '17 at 07:19