How to encrypt/change the name of file when uploading in CI to avoid overwriting

When you are creating a page where the file is uploaded in the form and these files are saved on your server. Sometimes it happens that same name files overwrites the files. To avoid this just add the below line of code.


$config['encrypt_name'] = TRUE;

Advertisements

How to remove index.php from url in CodeIgniter

Yes, you can remove the index.php from your url in your CodeIgniter web application.

You just need to add a .htaccess file in your web application folder.

Create a file and give it name .htaccess and save it in the root folder, and then copy the below block of code and paste it in the .htaccess file.


<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

One more change you have to do in your config.php file.

Go to :

application/config/config.php

and remove index.php from $config[‘index_page’]

All Done. Enjoy!

How to know CodeIgniter Version

To know the current version of CodeIgniter you are using, you just need to echo a constant.


<?php echo CI_VERSION; ?>

Another way to know the version of CodeIgniter is to check it manually in the file where the constant CI_VERSION is declared.

The constant CI_VERSION is declared in :

system/core/CodeIgniter.php

You can check it there as well if you don’t prefer or like the first option.

 

How to upload multiple files in CodeIgniter

View : (Save it with name upload.php in your views folder)


<?php echo form_open_multipart('upload/do_upload'); ?>

<?php echo $this->session->userdata('msg');

$this->session->set_userdata('msg', ''); ?>

<input type="file" name="file[]" id="file[]" required multiple="multiple">

<button type="submit" class="btn-input" name="submit" id="submit">Submit</button>

<?php echo form_close(); ?>

Controller : (Save it with name Upload.php in your controller folder)


public function do_upload() {

$filesCount = count($_FILES['file']['name']);

$uploadPath = 'uploads/';   /* uploads folder must be located in your root folder (along with application folder) */

if(!is_dir($uploadPath)) {

mkdir($uploadPath,0755,TRUE);

}

for($i = 0; $i < $filesCount; $i++){

$_FILES['userFile']['name'] = $_FILES['file']['name'][$i];

$_FILES['userFile']['type'] = $_FILES['file']['type'][$i];

$_FILES['userFile']['tmp_name'] = $_FILES['file']['tmp_name'][$i];

$_FILES['userFile']['error'] = $_FILES['file']['error'][$i];

$_FILES['userFile']['size'] = $_FILES['file']['size'][$i];

$config['upload_path'] = $uploadPath;

$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp|doc|pdf|docx|rtf';

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

$this->upload->initialize($config);

if($this->upload->do_upload('userFile')){

$fileData = $this->upload->data();

$uploadData[$i]['file_name'] = $fileData['file_name'];

$uploadData[$i]['created'] = date("Y-m-d H:i:s");

$uploadData[$i]['modified'] = date("Y-m-d H:i:s");

}

}

if(!empty($uploadData)){

$this->session->set_userdata('msg', 'Files Uploaded Successfully');

$this->session->set_userdata('status', 'success');

$this->load->view('upload', $uploadData);

}

else {

$this->session->set_userdata('msg', $this->upload->display_errors());

$this->session->set_userdata('status', 'fail');

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

}