- Download Codeigniter
- Set Base URL and Autoload Helpers
- Folder Creation
- Load Library
- Create controller file
- Set file upload Preferences
- Create view file
Download CodeIgniter
The first step in setting up CodeIgniter is to download it from the official website. You can download the latest version of CodeIgniter 3 from the following URL:
https://codeigniter.com/userguide3/installation/downloads.html
Extract the Files
Once you have downloaded CodeIgniter 3, you need to extract the files to your local machine in a particular folder
Set Base URL and Autoload Helpers
- Open the "application/config/config.php" file in your project
$config['base_url'] = 'http://localhost/codeigniter/';
To autoload a helper in CodeIgniter 3, you need to follow these steps:
- Open the "application/config/autoload.php" file in your CodeIgniter 3 project.
- Find the line that reads "$autoload['helper'] = array();" and add the name of the helper you want to autoload inside the array.
$autoload['helper'] = array('url','file');
Load Library
Next, open the autoload.php file and load the 'upload' library:
$this->load->library('upload');
Set file upload Preferences
I will also set the preferences for the file upload process through the controller function do_upload(). this function will contain the following code:
$config['upload_path'] = './uploads/images';$config['allowed_types'] = 'jpg|png|jpeg';$config['max_size'] = '2048';
Create new controller
<?phpclass Home extends CI_Controller{public function __construct(){/*call CodeIgniter's default Constructor*/parent::__construct();/*load database libray manually*/$this->load->database();}public function index(){/*load file upload view form*/$this->load->view('file_form');}public function file_upload(){$files = $_FILES;$_FILES['image']['name']= $files['image']['name'];$_FILES['image']['type']= $files['image']['type'];$_FILES['image']['tmp_name']=$files['image']['tmp_name'];$_FILES['image']['error']= $files['image']['error'];$_FILES['image']['size']= $files['image']['size'];$config['upload_path'] = './uploads/images';$config['allowed_types'] = 'jpg|png|jpeg';$config['max_size'] = '2048'; // Max size 2Mb$this->load->library('upload', $config);$this->upload->initialize($config);if (!$this->upload->do_upload('image')) {//log_message('error', 'Image Upload Error: ' . $this->upload->display_errors());print_r($this->upload->display_errors());exit;}else{$dataInfo = $this->upload->data();$image = $dataInfo['file_name'];print_r("File Upload Successful");exit;}}}?>
Create new view
- Create a form in your application that allows users to upload images.
- Add the "enctype" attribute to your form tag with the value "multipart/form-data".
<!DOCTYPE html><html><head><title>File Upload form</title></head><body><form method="post" action="<?= base_url() ?>index.php/home/file_upload" enctype="multipart/form-data"><table width="600" border="1" cellspacing="5" cellpadding="5"><tr><td>Choose File </td><td><input type="file" name="image" required/></td></tr><tr><td colspan="2" align="center"><input type="submit" name="save" value="Save Data" /></td></tr></table></form></body></html>
Finally enter your project url in browser: "http://localhost/codeigniter/index.php/home/index"
Conclusion
We hope this guide has been helpful. If you have any questions or need further assistance, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
0 Comments