1

I am uploading multiple dynamic images with codeigniter with the following code.

foreach ($_FILES as $key => $value) {   

    $imagePrefix = time();                                  

    if (!$this->upload->do_upload($key))
    {
        echo $errors = $this->upload->display_errors();
        return '';
    }
    else
    {
        $imagename = $imagePrefix.$value['name'];
        $insertImage = $this->db->query("Insert into `tbl_event_imges` (`iEventID`,`vImage`) values ('".$insertId."','".$imagename."')");
    }
}

When I upload an image to the specific folder this works fine; the issue is if a user uploads an image having same name, then it will automatically rename the image and upload to the folder, but what I want is to rename it by adding $imagePrefix that I have added in else part of the code and then want to upload the image with this name. But this is not working with me..

Any suggestion please?

3
  • are you getting any errors ? Commented Jul 10, 2015 at 10:01
  • Nope,i am trying to rename uploading file with use of key,,but not working Commented Jul 10, 2015 at 10:03
  • Codeigniter upload library is only for single file uploads only. Commented Jul 10, 2015 at 10:13

3 Answers 3

1

you need to provide configuration preferences to your upload function like so:

$imagePrefix = time(); 
$imagename = $imagePrefix.$value['name'];

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

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $imagename; // set the name here

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

if (!$this->upload->do_upload($key)){
    ...
}else{
    ...
}

Source: http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload#setting-preferences

Sign up to request clarification or add additional context in comments.

Comments

0

Just alternative, you can use CI's rename function to rename after file uploaded.

$image = $this->upload->data();
$file_path = $image ['file_path'];
$file = $image ['full_path'];
$file_ext = $image['file_ext'];
$final_file_name = time() . $image['file_name'] . $file_ext;

Comments

0

only add below code and it run successfully and also set encrypt_name to false

$path = $_FILES['userfile']['name'];
$newName = "Add any name".".".pathinfo($path, PATHINFO_EXTENSION);
$config['file_name'] = $newName; 
$config['encrypt_name'] = false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.