problems retrieving file extension in CI

﹥>﹥吖頭↗ 提交于 2020-01-03 04:47:10

问题


How can I retrieve the file extension of an image while uploading?

I don't have any problems with the upload, just retrieving the files extension , which would be useful when creating thumbnails dynamically.

Thanks


回答1:


http://codeigniter.com/user_guide/libraries/file_uploading.html

$this->upload->data()

This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

So after the user has uploaded something, you probably want to store the file extension in your database along with other details about the image :-)




回答2:


 $name_of_file_with_extn =  $this->upload->data('file_name')

you can change the item name from the below list

file_name Name of the file that was uploaded, including the filename extension file_type File MIME type identifier file_path Absolute server path to the file full_path Absolute server path, including the file name raw_name File name, without the extension orig_name Original file name. This is only useful if you use the encrypted name option. client_name File name as supplied by the client user agent, prior to any file name preparation or incrementing file_ext Filename extension, period included file_size File size in kilobytes is_image Whether the file is an image or not. 1 = image. 0 = not. image_width Image width image_height Image height image_type Image type (usually the file name extension without the period) image_size_str A string containing the width and height (useful to put into an image tag)

This might help you.




回答3:


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


来源:https://stackoverflow.com/questions/5256988/problems-retrieving-file-extension-in-ci

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