phpcakephpfile-uploadfile-extensioncakephp-2.1

get extension of uploaded file in cake php


I am using cakephp 2.1 and i am trying to upload files and how can i retrive the extension of the file.

Database/users

Id Auto_Increment
username
file_name

Controller/UsersController.php

public function register(){
            if ($this->request->is('post')){
                $filename = $this->data['User']['file_name']['name'];
                //$temp_ext = $this->data['User']['resume_file']['ext'];

                $this->Session->setFlash('Extension : ' . $temp_ext);
            }
}

When tried the above code, to get extension. it only gives single letters like L, r ie firt character of the filename but not extension

Now how can i get the extension of the file.. i gone through this link

http://api.cakephp.org/class/file

but could not understand to retrieve the file.


Adding a Debug report to @Julian Hollmann

array(
    'User' => array(
        'file_name' => array(
            'name' => '550992_234300256686731_213914803_n.jpg',
            'type' => 'image/jpeg',
            'tmp_name' => 'D:\xampp\tmp\php866F.tmp',
            'error' => (int) 0,
            'size' => (int) 42292
        )
    )
)

Solution

  • First of all, your data should be in $this->request->data

    If you want to see what's in there, just do debug($this->request->data);

    Edit: The correct answer is:

    $filename = $this->request->data['User']['file_name']['name'];
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    

    See also php manual