Codeigniter Ajax Image Upload with Preview Uploading files and images and pdf is a common practice nowadays, bet it Facebook, Instagram or any other website.
But do you know how to create this common feature of uploading image and display image's preview in the Codeigniter 4 application using the jQuery AJAX (Asynchronous JavaScript)
In this Codeigniter ajax image upload with preview tutorial, iwill explain how to create image upload , preview function in Codeigniter with the help of jQuery from scratch.
Codeigniter 4 Ajax Image Upload and Image Preview Example
- Step 1: Install Codeigniter App
- Step 2: Enable The Error Debugging
- Step 3: Create Database and Table
- Step 4: Add Database Details
- Step 5: Create AJAX Image Upload Controller
- Step 6: Create Route
- Step 7: Set Up File Upload View
- Step 8: Run Codeigniter Project
Install Codeigniter App
There are two ways to create Codeigniter app either you can install composer on your system and run the below command.
Make sure to rename the app name as per your choice.
Or, you can directly visit the Codeigniter website, download the latest version of the Codeigniter 4 app.
Enable Error Debugging
You need to go to app/Config/Boot/development.php and app/Config/Boot/production.php file and change the display_errors
property to 1 from 0.
Create Table Using SQL
In this section we need to understand how to create new table for file upload using the SQL. We assume you have already created the database. Run the below command in the SQL to create a new table into the database.
Add Database Details
In app/Config/Database.php file look for $default public variable,there you need to add the database name, user name and password.
CodeIgniter\Database\Exceptions\DatabaseException
There are a high chance, that you might get Unable to connect database : Codeigniter error, especially if you are working with MAMP or XAMP servers. You can define either of the hostname for XAMP.
Create AJAX Image Upload Controller
In next step, you have to create new controller file inside the app/Controllers folder; there, after open the app/Controllers/AjaxFileUpload.php file update the given below code.
The index()
function returns the view()
method pass the view template name inside the method. Secondly the upload() method validate image, with mime type, file size and store the file into the database.
Create Route
open app/Config/Routes.php file and then define the route that will allow you to open the CI app on the browser.
Set Up File Upload View
Next, head over to app/Views/ folder inside create the index.php file , update the following code inside the app/Views/index.php file.
Run Codeigniter Project
In this final segment of this guide, we have to start the CI development server this can be done by executing the below command.
Here is url that helps you upload the file.
Conclusion
Codeigniter AJAX image uploading is over; AJAX comes up with many quintessential features that enhance the user experience in web apps. It is superb for asynchronous processing and lowers server hits and network load, and most importantly and gives swift page rendering with enhanced response times.