Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc.

Loading the Download Helper

Before using the download function, you must load the helper in the controller’s files.

Syntax

Function of Download Helpers

  • force_download(): The function downloads the data forcefully in the system.

Syntax

Parameters

  • $filename: It defines the downloaded file.
  • $data: It denotes the file content.
  • $set_mime (bool): It sets the MIME value to true or false.

Download a run time file from the server

You can create a file that can download at run time from the server. To download a file from the server, create a Tests.php file in application/controller folder and write the following program.

Tests.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and, it downloads a text file named First.txt, as shown in the following image.

Download Helper Codeigniter

After downloading, click on the downloaded file and open with the notepad editor. It shows the content of the file.

Download Helper Codeigniter

Download Predefined file from the server

You can create a file that can download at run time from the server. To achieve the same, you need to create a Tests.php file in application/controller folder and write the following program.

Tests.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/, it downloads a file that is already exist in local system. We have downloaded a file named CSRF Attack in cakephp Solution.mp4.

Pin It on Pinterest

Share This