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 
Dhiraj Thakur
  • 338
  • 3
  • 11
Reaksmey
  • 205
  • 2
  • 7
  • 15
  • 1
    Possible duplicate of [How to extract a file extension in PHP?](http://stackoverflow.com/questions/173868/how-to-extract-a-file-extension-in-php) – Pathik Vejani Oct 13 '16 at 11:18
  • Refer this user guide : https://codeigniter.com/user_guide/libraries/file_uploading.html#preferences And apply using this answer . : http://stackoverflow.com/a/5257466/4952944 – Bhavin Oct 13 '16 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. – TimBrownlaw Oct 13 '16 at 11:23
  • @TimBrownlaw it work now echo $config['file_name'] . $this->upload->data('file_ext'); – Reaksmey Oct 13 '16 at 11:42
  • Good stuff :), you were this close! – TimBrownlaw Oct 13 '16 at 11:43
  • @TimBrownlaw u remind me this is fullname & ext : echo $this->upload->data('orig_name'); 0000_00000.jpg hehehe – Reaksmey Oct 13 '16 at 12:06
  • Even better. Now you are cooking! – TimBrownlaw Oct 13 '16 at 12:08

6 Answers6

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

Dien
  • 61
  • 2
  • 3
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);
James
  • 135
  • 3
  • 6
1

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

$config['file_name'].$this->upload->data('file_ext')
ray
  • 5,454
  • 1
  • 18
  • 40
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;
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
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.

Eddie Tower
  • 11
  • 1
  • 6
-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];
Muhammad Talha
  • 337
  • 5
  • 19