Codeigniter Rename file on upload
Asked Answered
K

11

38

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);
        }
    }
}
?>
Kalamazoo answered 16/2, 2014 at 13:30 Comment(1)
possible duplicate of Renaming an uploaded file in CodeIgniterEphah
S
105

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;
Spancel answered 16/2, 2014 at 13:46 Comment(0)
A
9

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);
Agapanthus answered 30/4, 2014 at 3:45 Comment(3)
i'm having the exact same issue... have you came across any solution???Pig
Found the solution to this problem. I'll put here in case if anyone looking for the solution. Solution is simple... Load the library in the constructor $this->load->library('upload'); Then initialize the function each time before upload, $this->upload->initialize($config); it'll work...Pig
I was facing this issue and resolved with your solution. Thanks!Jaye
S
3
        /* Conf */
        $new_name                   = time().$_FILES["userfiles"]['name'];
        $config['file_name']        = $new_name;
        $config['upload_path']      = './upload/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']         = '';
        $config['max_width']        = '';
        $config['max_height']       = '';
        $config['file_ext_tolower'] = TRUE;

      /*  Load the upload library  */   
        $this->load->library('upload', $config);
Selfdevotion answered 9/1, 2019 at 13:2 Comment(0)
D
2

For what you are exactly looking for, this worked for me:

$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION); 
Dahabeah answered 4/7, 2016 at 1:13 Comment(0)
K
1

when you use do_upload() function its rename file name if already exists and upload file

System->libraries->Upload.php

default function of do_upload() return true or false replace

return $this->upload_path.$this->file_name;

and in another file

 <input type='file' name='userfile'/>


<?php
    $upload_file_name = $this->upload->do_upload('userfile');
                $finalname;
                if (!$upload_file_name) {
                    $finalname = 'default.png';
                } else {

                    $split_data_upload_file_name = explode("/", $upload_file_name);
                    foreach ($split_data_upload_file_name as $i => $value) {

                        $finalname = $value;
                    }
                }

?>
Kettie answered 19/2, 2019 at 4:18 Comment(0)
E
0
$config['file_name'] = $new_name;

Just add it align with the config codes.

Erasion answered 4/3, 2016 at 18:6 Comment(0)
B
0

This should be after the upload.

Process the filename you want here:

$a=$this->upload->data();         
rename($a['full_path'],$a['file_path'].'file_name_you_want');
Burglary answered 6/5, 2016 at 9:13 Comment(0)
B
0

Try This:

$config['file_name'] = "yourCustomFileName";
Bronwynbronx answered 13/11, 2018 at 17:51 Comment(0)
N
0

If your $config is with this code: $config['encrypt_name'] = TRUE; the name of your file will be forced to rename with a encrypt name, just remove the code.

Nickolas answered 29/9, 2020 at 15:30 Comment(0)
B
0

Solution 1: Upload file with new customized and specific name (Note: this answer is applicable when you are submitting a unique name of a record, for example student name is a unique value in a record then you will use $_POST['student_name'])

$config['file_name'] = str_replace(' ', '_', $_POST['anyUniqueValue']);
//any Other unique value that you are using to submit with this file
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;

Solution 2: Upload file with new unique name

$config['file_name'] = time();
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;

Solution 3: Upload file with new unique name using codeigniter's default option

$config['encrypt_name'] = TRUE;
Bookmaker answered 31/1, 2021 at 19:42 Comment(0)
V
0

if you do multiple file upload, maybe you can try update library Upload.php on system/libraries/Upload.php. On function do_upload()

$this->file_name = $this->_prep_filename($_file['name']);

to

$this->file_name = time().'_'.$this->_prep_filename($_file['name']);
Vaunting answered 8/4, 2022 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.