How Can I Build a Database That Will Contain 4 Different Field and Photo Upload Otion

codeigniter file upload

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

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

  1. Binder Creation
  2. Create the Controller
  3. Set File Upload Preferences
  4. The Class Helper
  5. Structural View
  6. The Success Message
  7. Conclusion

Folder Cosmos

The first step is the cosmos of two folders that form the ground of the upload process. The outset folder is the destination folder that would receive the uploaded files.

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

Go to the view binder (located at the root of the CI installation)  and create two 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).

Nil equally Easy as Deploying PHP Apps on Cloud

With Cloudways, y'all tin can take your PHP apps up and running on managed deject servers in just a few minutes.

Create the Controller

The next step is the creation of a file in the controller binder.  Name the file upload_controller.php. In this file, I volition load a library for initializing the Upload form through the following code:

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

Fix File Upload Preferences

I will also set up the preferences for the file upload process through the controller partdo_upload(). this function will comprise 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';        

Equally you could see, through this part, you could easily restrict the upload path, allowed file types, maximum size and dimensions of the epitome.

yous could come across this office in action when you lot call the file_view.php file. if you are at a staging domain, use the URL: your-domain/ci_demo/index.php/upload_controller/file_view OR in the instance 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 grade. For this, I have included a form helper in the controller with the post-obit syntax:

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

You Might Also Like: Elementary Guide to CodeIgniter Course Validation

Structural View

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

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

Side by side, go to the controller binder and create a new file with the proper noun upload_controller.php

You might as well like: How To Pass Data From Controller To View In CodeIgniter

Add the following code to this file:

<?php if ( ! defined('BASEPATH')) get out('No direct script access allowed'); class Upload_Controller extends CI_Controller { public role __construct() { parent::__construct(); } public role custom_view(){ $this->load->view('custom_view', array('error' => ' ' )); } public function do_upload(){ $config = array( 'upload_path' => "./uploads/", 'allowed_types' => "gif|jpg|png|jpeg|pdf", 'overwrite' => TRUE, 'max_size' => "2048000", // Tin can exist fix to particular file size , hither it is ii MB(2048 Kb) 'max_height' => "768", 'max_width' => "1024" ); $this->load->library('upload', $config); if($this->upload->do_upload()) { $data = assortment('upload_data' => $this->upload->data()); $this->load->view('upload_success',$data); } else { $error = array('mistake' => $this->upload->display_errors()); $this->load->view('custom_view', $fault); } } } ?>        

Detect that the array $config of the do_upload() has an chemical element named allowed_types. You could apply the parameters of this chemical element to alter the allowed file types. For instance, to let a range of files, employ the following structure of this element:

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

The Success Bulletin

To brandish the upload successful message, I will use the upload_success.php file (located in the view folder). Add together the post-obit lawmaking to it:

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

Notation: Make certain that you are calling the correct path for the controller course

Related: How To Create A Rest API In CodeIgniter

Conclusion

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

If you lot demand help in agreement the code or integrating the code into your CI application, do get out a annotate below.

Share your stance in the comment section. COMMENT Now

Share This Commodity

Customer Review at

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

Sanjit C [Website Developer]

Owais Alam

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

ibarrawillieret.blogspot.com

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

0 Response to "How Can I Build a Database That Will Contain 4 Different Field and Photo Upload Otion"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel