get extension of uploaded file in cake php

删除回忆录丶 提交于 2019-12-08 11:08:26

问题


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
        )
    )
)

回答1:


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




回答2:


$filename = $this->data['User']['file_name']['name'];
$this->request->data['User']['file_name'] = $filename;

$fileExt = explode(".", $filename);
$fileExt2 = end($fileExt);

Try this one... it will give you extension



来源:https://stackoverflow.com/questions/11031567/get-extension-of-uploaded-file-in-cake-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!