Javascript required
Skip to content Skip to sidebar Skip to footer

Correct File Uploaded to Server but Not in Inspect Source

codeigniter file upload

File uploading in CodeIgniter has two main parts—the frontend and the backend. The frontend is taken care of by the HTML grade that uses the form input blazon file. On the backend, the file upload library forms the submitted input from the form 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 could validate and fifty-fifty restrict file size and type during the upload process.

  1. Binder Cosmos
  2. Create the Controller
  3. Set File Upload Preferences
  4. The Form Helper
  5. Structural View
  6. The Success Bulletin
  7. Decision

Folder Creation

The first stride is the creation of two folders that form the basis of the upload procedure. The first folder is the destination binder that would receive the uploaded files.

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

Become to the view binder (located at the root of the CI installation)  and create 2 new "view" files. Name these files file_view.php (displays the course containing file upload fields) and upload_success.php (displays the upload successful bulletin).

Nothing equally Easy as Deploying PHP Apps on Cloud

With Cloudways, y'all tin have your PHP apps upward and running on managed cloud servers in just a few minutes.

Create the Controller

The next step is the cosmos 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 lawmaking:

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

Fix File Upload Preferences

I will too set the preferences for the file upload process through the controller functiondo_upload(). this function volition contain 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 you could see, through this role, y'all could easily restrict the upload path, immune file types, maximum size and dimensions of the image.

y'all could see this function in action when you call the file_view.php file. if y'all are at a staging domain, employ the URL: your-domain/ci_demo/alphabetize.php/upload_controller/file_view OR in the case of loclhost: localhost/ci_demo/index.php/upload_controller/file_view

Related: How To Host CodeIgniter PHP On Deject

The Course Helper

Notation that file upload requires a multipart form. For this, I have included a form helper in the controller with the post-obit syntax:

$this->load->helper(assortment('course', 'url'));

You Might Besides Like: Simple Guide to CodeIgniter Form Validation

Structural View

For the structure of the paradigm upload, I will get-go by creating a file in the views binder  with the name custom_view.php. Open this file and add the post-obit code to it:

<html> <head>     <title>File Upload In Codeigniter</title> </head> <trunk> <?php echo $error;?>  <?php echo form_open_multipart('upload_controller/do_upload');?> <?php repeat "<input blazon='file' proper noun='userfile' size='20' />"; ?> <?php repeat "<input blazon='submit' name='submit' value='upload' /> ";?> <?php repeat "</grade>"?> </body> </html>        

Next, get to the controller folder and create a new file with the name upload_controller.php

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

Add the following code to this file:

<?php if ( ! defined('BASEPATH')) exit('No direct script admission allowed'); class Upload_Controller extends CI_Controller { public function __construct() { parent::__construct(); } public function custom_view(){ $this->load->view('custom_view', array('error' => ' ' )); } public role do_upload(){ $config = array( 'upload_path' => "./uploads/", 'allowed_types' => "gif|jpg|png|jpeg|pdf", 'overwrite' => TRUE, 'max_size' => "2048000", // Tin can exist prepare to particular 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 = assortment('upload_data' => $this->upload->data()); $this->load->view('upload_success',$data); } else { $mistake = array('error' => $this->upload->display_errors()); $this->load->view('custom_view', $error); } } } ?>        

Notice that the array $config of the do_upload() has an element named allowed_types. You could use the parameters of this element to change the allowed file types. For example, to permit a range of files, use the following structure of this chemical element:

'allowed_types' => "gif|jpg|jpeg|png|iso|dmg|zip|rar|medico|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 volition use the upload_success.php file (located in the view binder). Add the following lawmaking to information technology:

<html> <caput>     <championship>Success Bulletin</title> </head> <body> <h3>Congragulation You Accept Successfuly Uploaded</h3> <!-- Uploaded file specification will prove up here --> <ul>     <?php foreach ($upload_data as $detail => $value):?>     <li><?php echo $particular;?>: <?php echo $value;?></li>     <?php endforeach; ?> </ul> <p><?php repeat anchor('upload_controller/file_view', 'Upload Some other File!'); ?></p> </trunk> </html>        

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

Related: How To Create A Residual API In CodeIgniter

Conclusion

This was a brusque tutorial on creating an image and file upload procedure in a CodeIgniter powered application. Call back that you could hands alter the immune file types and image parameters through the lawmaking.

If you lot demand help in understanding the code or integrating the lawmaking into your CI awarding, do leave a comment below.

Share your opinion in the comment section. COMMENT At present

Share This Commodity

Customer Review at

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

Sanjit C [Website Developer]

Owais Alam

is the WordPress Customs Manager 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 can email him at [email protected]

merionhicip1942.blogspot.com

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