在cake php中获取上传文件的扩展名 [英] get extension of uploaded file in cake php
问题描述
我使用的是cakephp 2.1,我正在尝试上传文件,以及如何获取文件的扩展名。
I am using cakephp 2.1 and i am trying to upload files and how can i retrive the extension of the file.
Id Auto_Increment
username
file_name
Controller / UsersController.php
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);
}
}
尝试上述代码时,它只给出单个字母像L,r即firt字符的文件名,但不是扩展
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
但无法检索该文件。
向@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
)
)
)
推荐答案
首先,您的数据应在 $ this-> request-> data
如果你想看看里面有什么,只需要 debug($ this-> request-> data);
If you want to see what's in there, just do debug($this->request->data);
编辑:
正确的答案是:
The correct answer is:
$filename = $this->request->data['User']['file_name']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
另请参阅: http://php.net/manual/en/function.pathinfo.php
这篇关于在cake php中获取上传文件的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!