The $_FILES
superglobal array allows users to get the file information including the file name, file type, file size, and file extension. In this tutorial, you will learn how to get the file name, type, and extension before uploading a file in CodeIgniter 4, 3.
How to Get File Name, File type, Extension Before Upload in CodeIgniter
Here are the steps to get the file name, file type, and extension before uploading in CodeIgniter 4, 3:
- Step 1: Create a form to upload the file
- Step 2: Handle the file upload request
- Step 3: Get the file information
- Step 4: Get the file extension
- Step 5: Rename the file
- Step 6: Move the file to the server
- Step 7: Display file information to the user
Step 1: Create a form to upload the file
Create a form with input field that allows users to choose a file for upload.
<form method="post" action="<?php echo base_url('upload'); ?>" enctype="multipart/form-data"> <input type="file" name="userfile"> <input type="submit" name="submit" value="Upload"> </form>
Step 2: Handle the file upload request
To handle the file upload request, create method in controller.
public function upload() {
// Code to handle file upload
}
Step 3: Get the file information
In the controller method, you can use the $_FILES
superglobal array to get the file information including the file name, file type, and file size:
public function upload() { $file_name = $_FILES['userfile']['name']; $file_type = $_FILES['userfile']['type']; $file_size = $_FILES['userfile']['size']; // Code to handle file upload }
Step 4: Get the file extension
To get the file extension, you can use the pathinfo()
function:
public function upload() { $file_name = $_FILES['userfile']['name']; $file_type = $_FILES['userfile']['type']; $file_size = $_FILES['userfile']['size']; $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); // Code to handle file upload }
Step 5: Rename the file
To rename the file before uploading it to the server, Use uniqid()
function to generate a unique name for the file:
public function upload() { $file_name = $_FILES['userfile']['name']; $file_type = $_FILES['userfile']['type']; $file_size = $_FILES['userfile']['size']; $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); $new_file_name = uniqid().'.'.$file_ext; // Code to handle file upload }
Step 6: Move the file to the server
Use the move_uploaded_file()
function to move the file to folder or directory:
public function upload()
{
// Retrieve file information
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['userfile']['size'];
$file_ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// Upload file to server
move_uploaded_file($_FILES['file']['tmp_name'], './uploads/' . $file_name);
}
Step 7: Display file information to the user
To show uploaded file information. Here is an example code:
<h2>File Upload Results</h2>
<p>File name: <?php echo $file_name; ?></p>
<p>File type: <?php echo $file_type; ?></p>
<p>File extension: <?php echo $file_ext; ?></p>
Conclusion
This tutorial taught you how to get file names, types, and extensions in CodeIgniter applications.
sir mujhe razorpay payment getway add karna he database ke sath kay aap code batha sakte he me apka website par check kiya par database ke sath nahi tha jis me tha wah codeigniter 3 me he jo 4 version in nahi kam kar raha he.
Pleace help….