Skip to content Skip to sidebar Skip to footer

Codeigniter Upload Photo With Other Form Inputs Same Time

codeigniter file upload

File uploading in CodeIgniter has 2 primary parts—the frontend and the backend. The frontend is taken intendance of by the HTML class that uses the form input type file. On the backend, the file upload library forms the submitted input from the form and writes information technology to the transfer registry.

This tutorial discusses the process of creating a CodeIgniter based file upload component that could exist easily used to upload images and other files with ease. You could validate and fifty-fifty restrict file size and type during the upload procedure.

  1. Folder Creation
  2. Create the Controller
  3. Fix File Upload Preferences
  4. The Form Helper
  5. Structural View
  6. The Success Message
  7. Determination

Folder Creation

The first footstep is the cosmos of 2 folders that form the footing of the upload process. The get-go folder is the destination folder that would receive the uploaded files.

Next, go to the root of the CI installation and create a folder named "uploads".

Go to the view folder (located at the root of the CI installation)  and create ii new "view" files. Name these files file_view.php (displays the form containing file upload fields) and upload_success.php (displays the upload successful message).

Nada every bit Like shooting fish in a barrel as Deploying PHP Apps on Cloud

With Cloudways, you can have your PHP apps up and running on managed cloud servers in just a few minutes.

Create the Controller

The adjacent step is the creation of a file in the controller folder.  Name the file upload_controller.php. In this file, I will load a library for initializing the Upload class through the following code:

$this->load->library('upload');

Set File Upload Preferences

I will too fix the preferences for the file upload process through the controller roledo_upload(). this function will incorporate the following code:

$config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768';        

As yous could see, through this function, you could easily restrict the upload path, allowed file types, maximum size and dimensions of the image.

yous could run across this office in action when you call the file_view.php file. if yous are at a staging domain, use the URL: your-domain/ci_demo/alphabetize.php/upload_controller/file_view OR in the example of loclhost: localhost/ci_demo/index.php/upload_controller/file_view

Related: How To Host CodeIgniter PHP On Cloud

The Form Helper

Annotation that file upload requires a multipart form. For this, I have included a form helper in the controller with the following syntax:

$this->load->helper(array('class', 'url'));

You Might Also Like: Simple Guide to CodeIgniter Grade Validation

Structural View

For the structure of the image upload, I will start by creating a file in the views binder  with the name custom_view.php. Open this file and add together the following code to it:

<html> <caput>     <title>File Upload In Codeigniter</title> </head> <torso> <?php repeat $error;?>  <?php echo form_open_multipart('upload_controller/do_upload');?> <?php echo "<input type='file' name='userfile' size='xx' />"; ?> <?php echo "<input type='submit' proper noun='submit' value='upload' /> ";?> <?php echo "</course>"?> </torso> </html>        

Side by side, go to the controller folder and create a new file with the name upload_controller.php

You might also like: How To Laissez passer Information From Controller To View In CodeIgniter

Add the following lawmaking to this file:

<?php if ( ! defined('BASEPATH')) exit('No direct script access immune'); class Upload_Controller extends CI_Controller { public office __construct() { parent::__construct(); } public function custom_view(){ $this->load->view('custom_view', array('error' => ' ' )); } public office do_upload(){ $config = array( 'upload_path' => "./uploads/", 'allowed_types' => "gif|jpg|png|jpeg|pdf", 'overwrite' => Truthful, 'max_size' => "2048000", // Can be set to item file size , here it is 2 MB(2048 Kb) 'max_height' => "768", 'max_width' => "1024" ); $this->load->library('upload', $config); if($this->upload->do_upload()) { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success',$data); } else { $error = assortment('error' => $this->upload->display_errors()); $this->load->view('custom_view', $mistake); } } } ?>        

Notice that the array $config of the do_upload() has an element named allowed_types. You lot could employ the parameters of this element to change the immune file types. For case, to allow a range of files, use the following construction of this element:

'allowed_types' => "gif|jpg|jpeg|png|iso|dmg|zip|rar|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|sxc|sxi|txt|exe|avi|mpeg|mp3|mp4|3gp",

The Success Message

To display the upload successful message, I will utilize the upload_success.php file (located in the view folder). Add together the following lawmaking to it:

<html> <head>     <title>Success Bulletin</championship> </head> <trunk> <h3>Congragulation You Take Successfuly Uploaded</h3> <!-- Uploaded file specification will bear witness up here --> <ul>     <?php foreach ($upload_data as $item => $value):?>     <li><?php echo $item;?>: <?php echo $value;?></li>     <?php endforeach; ?> </ul> <p><?php repeat anchor('upload_controller/file_view', 'Upload Another File!'); ?></p> </torso> </html>        

Note: Brand sure that you lot are calling the right path for the controller class

Related: How To Create A Rest API In CodeIgniter

Determination

This was a brusque tutorial on creating an image and file upload process in a CodeIgniter powered awarding. Retrieve that you could easily modify the allowed file types and prototype parameters through the lawmaking.

If yous need help in understanding the code or integrating the lawmaking into your CI application, exercise leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

"Cloudways hosting has one of the best customer service and hosting speed"

Sanjit C [Website Developer]

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP programmer. He loves to develop all sorts of websites on WordPress and is in dear with WooCommerce in particular. You can email him at [email protected]

rogersherus1950.blogspot.com

Source: https://www.cloudways.com/blog/codeigniter-upload-file-image/

Post a Comment for "Codeigniter Upload Photo With Other Form Inputs Same Time"