How to Upload Photos on Php Website With Guide

codeigniter file upload

File uploading in CodeIgniter has 2 master parts—the frontend and the backend. The frontend is taken intendance of by the HTML course that uses the grade input blazon 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 even 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 Message
  7. Conclusion

Folder Creation

The first step is the creation of ii folders that course the ground of the upload process. The starting time folder is the destination binder that would receive the uploaded files.

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

Become to the view binder (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).

Nothing as Like shooting fish in a barrel equally Deploying PHP Apps on Cloud

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

Create the Controller

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

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

Set File Upload Preferences

I will also set the preferences for the file upload procedure through the controller functiondo_upload(). this function volition incorporate the post-obit 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 lot could meet, through this office, yous could hands restrict the upload path, allowed file types, maximum size and dimensions of the image.

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

Related: How To Host CodeIgniter PHP On Cloud

The Class Helper

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

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

You Might Also Similar: Simple Guide to CodeIgniter Form Validation

Structural View

For the construction of the image upload, I will get-go by creating a file in the views folder  with the proper noun custom_view.php. Open this file and add the following lawmaking to it:

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

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

Yous might also like: How To Laissez passer Information 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 role __construct() { parent::__construct(); } public function custom_view(){ $this->load->view('custom_view', array('error' => ' ' )); } public part do_upload(){ $config = array( 'upload_path' => "./uploads/", 'allowed_types' => "gif|jpg|png|jpeg|pdf", 'overwrite' => TRUE, 'max_size' => "2048000", // Can be set 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 = array('upload_data' => $this->upload->data()); $this->load->view('upload_success',$data); } else { $error = array('mistake' => $this->upload->display_errors()); $this->load->view('custom_view', $error); } } } ?>        

Find 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 immune file types. For case, to allow a range of files, use the post-obit 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 Message

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

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

Note: Make certain that yous are calling the correct path for the controller class

Related: How To Create A REST API In CodeIgniter

Conclusion

This was a short tutorial on creating an image and file upload process in a CodeIgniter powered application. Recollect that yous could easily alter the allowed file types and epitome parameters through the lawmaking.

If you need help in understanding the code or integrating the code into your CI awarding, practice exit a comment below.

Share your opinion in the comment department. Comment Now

Share This Article

Customer Review at

"Cloudways hosting has one of the all-time client 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 developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can e-mail him at [email protected]

colearither65.blogspot.com

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

0 Response to "How to Upload Photos on Php Website With Guide"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel