Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Rename file on upload

I'm trying to add time as the prefix of the image name along with the original name when uploading, But I couldn't figure it out. Please help me with the following code to add a prefix to my original file name when uploading.

<?php  class Upload extends CI_Controller {      function __construct()     {         parent::__construct();         $this->load->helper(array('form', 'url'));     }      function index()     {         $this->load->view('upload_form', array('error' => ' ' ));     }      function do_upload()     {           $config['upload_path'] = 'Public/uploads/';         $config['allowed_types'] = 'gif|jpg|png';         $config['max_size'] = '1024';         $config['max_width']  = '1024';         $config['max_height']  = '768';            $this->load->library('upload', $config);           if ( ! $this->upload->do_upload())         {             $error = array('error' => $this->upload->display_errors());              $this->load->view('upload_form', $error);         }         else         {             $data = array('upload_data' => $this->upload->data());              $this->load->view('upload_success', $data);         }     } } ?> 
like image 821
Dilukshan Mahendra Avatar asked Feb 16 '14 13:02

Dilukshan Mahendra


People also ask

How to change file name while uploading in codeigniter?

php class Upload extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } function index() { $this->load->view('upload_form', array('error' => ' ' )); } function do_upload() { $config['upload_path'] = 'Public/uploads/'; $config['allowed_types'] = 'gif|jpg|png' ...

How to increase upload file size in codeigniter?

if you want to set bigger upload size to your system, you need to go to php. ini file and change this. upload_max_filesize = 2M; //change this to your desired size.

How to upload pdf file in codeigniter?

For Uploading files, as usually we need a Simple HTML form, with an input field and submit button. Here is the Code: 'file','name' => 'userfile')); echo form_submit('submit','upload'); echo form_close(); ?>


2 Answers

You can encrypt file name with use of CI native option:

$config['encrypt_name'] = TRUE; 

OR

You can do it with your own code:

$new_name = time().$_FILES["userfiles"]['name']; $config['file_name'] = $new_name; 
like image 148
Kumar V Avatar answered Sep 24 '22 05:09

Kumar V


For some reasons, consecutive calls to the do_upload function doesn't work. It sticks to the first filename set by the first function call

$small_photo_url  = $this->upload_photo('picture_small',  $this->next_id.'_small '); $medium_photo_url = $this->upload_photo('picture_medium', $this->next_id.'_medium'); $large_photo_url  = $this->upload_photo('picture_large',  $this->next_id.'_large '); 

The filenames will all be "00001_small", "00001_small1", "00001_small2" given the following configurations

function upload_photo($field_name, $filename) {     $config['upload_path'] = 'Public/uploads/';     $config['allowed_types'] = 'gif|jpg|png';     $config['max_size'] = '1024';     $config['max_width']  = '1024';     $config['max_height']  = '768';     $config['file_name'] = $filename;      $this->load->library('upload', $config);      if ( ! $this->upload->do_upload())... 

I think it's because this line doesn't work the second time you call it. It does not set the configurations again

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

========================================================================== Solution to the problem faced during consecutive do_upload function calls:

// re-initialize upload library $this->upload->initialize($config); 
like image 24
Carmela Avatar answered Sep 21 '22 05:09

Carmela