How Can I Upload File In Codeigniter
CodeIgniter File Upload
File management is essential to most web applications. If you are developing a content management organisation, and so you lot will need to be able to upload images, word documents, pdf reports, etc. If yous are working on a membership site, you may demand to accept a provision for people to upload their profile images. CodeIgniter's File Uploading class makes it easy for u.s. to do all of the above.
In this tutorial, we will look at how to use the file upload library to load files.
Uploading Images in CodeIgniter
File uploading in CodeIgniter has two principal parts. The frontend and the backend. The frontend is handled by the HTML form that uses the form input blazon file. On the backend, the file upload library processes the submitted input from the class and writes it to the upload directory.
Let's begin with the input grade.
Create a new directory called files in application/views directory
Add the following files in application/views/files
- upload_form.php – this view contains the HTML class that has the input type of file and submits the selected file to the server for processing
- upload_result.php – this view displays the results of the uploaded image including a link that nosotros can click to view the results.
Add the following code to upload_form.php
<!DOCTYPE html> <html> <caput> <title>CodeIgniter Image Upload</championship> <meta charset="UTF-viii"> <meta proper noun="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div> <h3>Select an image from your reckoner and upload it to the cloud</h3> <?php if (isset($error)){ repeat $error; } ?> <form method="post" activeness="<?=base_url('store-image')?>" enctype="multipart/form-data"> <input type="file" id="profile_image" name="profile_image" size="33" /> <input blazon="submit" value="Upload Prototype" /> </grade> </div> </body> </html>
HERE,
- if (isset($mistake)){…} checks if the error variable has been set. If the outcome is true and then the error returned past the upload library is displayed to the user.
- <input type="file" id="profile_image" name="profile_image" size="33″ /> the type file allows the user to browser to their figurer and select a file for uploading.
Advertizement the following lawmaking to upload_result.php
<!DOCTYPE html> <html> <head> <title>Paradigm Upload Results</title> <meta charset="UTF-eight"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div> <h3>Congratulations, the paradigm has successfully been uploaded</h3> <p>Click here to view the image y'all just uploaded <?=anchor('images/'.$image_metadata['file_name'], 'View My Paradigm!')?> </p> <p> <?php echo anchor('upload-image', 'Go dorsum to Prototype Upload'); ?> </p> </div> </body> </html>
HERE,
- <?=anchor('images/'.$image_metadata['file_name'], 'View My Prototype!')?> uses the anchor helper to create a link to the new uploaded file in the images directory. The proper name is retrieved from the prototype metadata that is passed to the view when the file has successfully been uploaded.
Let's now create the controller that will respond to our image uploading
Add together a new file ImageUploadController.php in application/controllers
Add the following code to ImageUploadController.php
<?php divers('BASEPATH') OR exit('No direct script access allowed'); course ImageUploadController extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url', 'class'); } public part alphabetize() { $this->load->view('files/upload_form'); } public function store() { $config['upload_path'] = './images/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 2000; $config['max_width'] = 1500; $config['max_height'] = 1500; $this->load->library('upload', $config); if (!$this->upload->do_upload('profile_image')) { $mistake = assortment('mistake' => $this->upload->display_errors()); $this->load->view('files/upload_form', $fault); } else { $data = array('image_metadata' => $this->upload->data()); $this->load->view('files/upload_result', $data); } } }
HERE,
- grade ImageUploadController extends CI_Controller {…} defines our controller form and extends the base controller CI_Controller
- public function __construct() {…} initializes the parent constructor method and loads the url and class helpers
- public function index() {…} defines the alphabetize method that is used to display the paradigm upload form
- public function store() {…} defines the method that will upload the image and store it on the web application server.
- $config['upload_path'] = './images/'; sets the directory where the images should be uploaded
- $config['allowed_types'] = 'gif|jpg|png'; defines the acceptable file extensions. This is important for security reasons. The allowed types ensures that but images are uploaded and other file types such equally php cant be uploaded because they take the potential to compromise the server.
- $config['max_size'] = 2000; set the maximum file size in kilobytes. In our instance, the maximum file that can exist uploaded is 2,000kb close to 2MB. If the user tries to upload a file larger than two,000kb, then the paradigm volition fail to upload and the library volition return an error message.
- $config['max_width'] = 1500; sets the maximum width of the image which in our case is i,500 px. Any width larger than that results in an error
- $config['max_height'] = 1500; defines the maximum acceptable height.
- $this->load->library('upload', $config); loads the upload library and initializes it with the array $config that we defined to a higher place.
- if (!$this->upload->do_upload('profile_image')) {…} attempts to upload the submitted image which in our case is named profile_image
- $error = array('error' => $this->upload->display_errors()); sets the fault message if the upload fails
- $this->load->view('files/upload_form', $error); loads the file upload class and displays the error message that is returned from the upload library
- $data = array('image_metadata' => $this->upload->data()); sets the epitome metadata if the upload has been successful
- $this->load->view('files/upload_result', $data); loads the uploaded successfully view and passes the uploaded file metadata.
That is it for the controller. Let's now create the directory where our images will exist uploaded to. Create a new directory "images" in the root directory of your application
Finally, we will ad two routes to our routes.php file that will display the class and brandish results
Open up application/config/routes.php Add the following routes $route['upload-prototype'] = 'imageuploadcontroller'; $route['store-image'] = 'imageuploadcontroller/store';
HERE,
- $route['upload-prototype'] = 'imageuploadcontroller'; defines the URL upload-paradigm that calls the index method of ImageUploadController
- $route['shop-prototype'] = 'imageuploadcontroller/store'; defines the URL shop-image that accepts the selected user file and uploads it to the server.
Testing the application
Let's first the congenital-in PHP server
Open the terminal/ control line and browse to the root of your application. In my example, the root is located in drive C:\Sites\ci-app
cd C:\Sites\ci-app
start the server using the following control
php -S localhost:3000
Load the post-obit URL in your spider web browser: http://localhost:3000/upload-image
you will be able to see the following results
Click on choose file
You should be able to see a dialog window similar to the post-obit
Select your desired paradigm then click on open up
The selected file name will show upwardly in the form upload as shown in the image in a higher place. Click on an upload image button
Y'all will get the post-obit results assuming everything goes well
Click on View My Image! Link
You should exist able to encounter the epitome that you uploaded. The results will be similar to the following
Detect uploaded image name is displayed in the URL. Nosotros got the image name from the uploaded paradigm metadata
Note: The File Upload process remains the aforementioned for other types of files
How Can I Upload File In Codeigniter,
Source: https://www.guru99.com/codeigniter-file-upload.html
Posted by: houstonallond.blogspot.com
0 Response to "How Can I Upload File In Codeigniter"
Post a Comment