How to rename file on upload in codeigniter?

Today, We want to share with you rename file upload codeigniter.In this post we will show you Renaming an uploaded file in CodeIgniter, hear for codeigniter get filename before upload we will give you demo and example for implement.In this post, we will learn about Laravel Renaming Uploaded Files Automatically with an example.

Dynamically Codeigniter Image Upload Rename

Step 1.

Create a codeigniter” controller for “Rename a file while uploading in codeigniter”.

Example 1: Change controller
Use below in your own controller.

class Test extends CI_Controller {

	public function index()
		$this->load->library('upload', $config); 
			$image = time().'-'.$_FILES["image"]['name']; //Filename
			$config = array(
						'upload_path' => "./uploads/",   //here folder name added Upload file Destination 
						'allowed_types' => "gif|jpg|png|jpeg|JPEG|JPG|PNG|GIF",  //some added Accepted types
						'overwrite' => TRUE,
						'max_size' => "1024",    //here set your Maximum upload file size
						'max_height' => "985",
						'max_width' => "1600",
						'file_name' => $image
				$result['msg']="Good Luck, File uploaded successfully!";
			} else {
				$result['msg']="Sorry, Error in upload file.";
			$this->load->view('image', $result);

encrypt file name with use of CI native option:

$config['encrypt_name'] = TRUE;

your own source code:

$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;

Codeigniter Rename file on upload

In CodeIgniter, you can easily rename a file during upload by specifying a new file name in the upload() method. Here’s an example:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['file_name'] = 'new_file_name.jpg';

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

if ($this->upload->do_upload('file')) {
    // File uploaded successfully
} else {
    // Error uploading file

In this example, we’re creating a new $config array with the upload configuration, including the desired upload path, allowed file types, maximum file size, and the new file name we want to use. Then we’re initializing the upload library with the configuration and calling the do_upload() method to upload the file. If the file is uploaded successfully, we can process it as needed. If there’s an error uploading the file, we can handle the error.

Note that if you don’t specify a new file name, CodeIgniter will use the original file name by default. If you want to rename the file based on some dynamic value, you can generate a new file name using PHP and assign it to the $config[‘file_name’] property before calling do_upload(). For example:

$new_file_name = 'prefix_' . time() . '.jpg';
$config['file_name'] = $new_file_name;

This will create a new file name with a prefix and a timestamp, which will be unique for each upload.

I hope you get an idea about Rename file upload codeigniter.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment