2

i need to store only filename + extension of file when upload and rename done to my databse.my problem is can't get extension.

    $new_file_name=date("mdY")."_".time();
    $config['upload_path']          = './assets/images/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 2048;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;
    $config['file_name']            = $new_file_name;

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

    if ( ! $this->upload->do_upload('uploadFile'))
    {
            $error = array('error' => $this->upload->display_errors());

            var_dump($error);
    }
    else
    {
            $data = array('upload_data' => $this->upload->data());
            var_dump($data);
            echo $config['file_name'] . $config['file_ext'];

    }

my next question load helper form for ? or need load form to use library form_validation ?

$this->load->helper(array('form')); for ?

$this->load->library('form_validation'); this for validation rule 
7
  • 1
    Possible duplicate of How to extract a file extension in PHP? Commented Oct 13, 2016 at 11:18
  • Refer this user guide : codeigniter.com/user_guide/libraries/… And apply using this answer . : stackoverflow.com/a/5257466/4952944 Commented Oct 13, 2016 at 11:20
  • You should be seeing all the File attributes from your var_dump($data) which you created from the $this->upload->data(). So you just need to grab what you want from that. Commented Oct 13, 2016 at 11:23
  • @TimBrownlaw it work now echo $config['file_name'] . $this->upload->data('file_ext'); Commented Oct 13, 2016 at 11:42
  • Good stuff :), you were this close! Commented Oct 13, 2016 at 11:43

6 Answers 6

6

custom upload filename codeigniter
first you can to explode with (.) in filename, then you get array, then catch the end array with function end().
and last i can to join filename with this date.

$dname = explode(".", $_FILES['gambar']['name']);
$ext = end($dname);
$_FILES['gambar']['name'] = strtolower('fitur_h_'.date('YmdHis').'.'.$ext);

//fitur_h_20200120143157.png  //output filename

i hope my answer can help you

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

Comments

2

After you uploaded the file, you can get its property by:

$saved_file_name = $this->upload->data('file_name');
// will give you the filename along with the extension

If you want to get the file extension before uploading it, use core php:

$file_ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);

or to make it clean

$filename= $_FILES["file"]["name"];
$file_ext = pathinfo($filename,PATHINFO_EXTENSION);

Comments

1

Simply use the below code for image name with extension on post to solve issue,

$config['file_name'].$this->upload->data('file_ext')

Comments

1

I know this is too late, still I'm giving the answer.

If you want to get the image extension, then use pathinfo function with PATHINFO_EXTENSION parameter. eg.

$imageExtention = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);
echo imageExtention;

Comments

0

I was having the same problem and i just figured it out.

I was trying to get the name with this: $this->upload->data('file_name'); but it was always empty, and i just finded out that you need to set something before you can access the data() array.

When your code successfully uploads the file, type this: $data = array('upload_data' => $this->upload->data());

after that, all the data() indexes will be accessible, 'file_name' included. The list with all indexes is in the documentation: https://codeigniter.com/user_guide/libraries/file_uploading.html#preferences

ps: I know it's been 2 years since this post but maybe someone can find it helpful if they stuck with this too.

Comments

-3

you can get file name using this code

$file_name = $data['file_name'];
echo $file_name;

and extension for file

$ext = implode('.',$file_name);
echo $ext[1];

2 Comments

it is explode not implode
and it won't work if file name has "."(dot). for example: abc.xyz.jpg

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.