在CI中检索文件扩展名时出现问题

时间:2011-03-10 08:08:53

标签: codeigniter file-upload

如何在上传时检索图像的文件扩展名?

上传时没有任何问题,只需检索文件扩展名,这在动态创建缩略图时非常有用。

由于

3 个答案:

答案 0 :(得分:5)

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

$this->upload->data()

这是一个辅助函数,它返回一个数组,其中包含与您上传的文件相关的所有数据。这是数组原型:

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

因此,在用户上传了某些内容后,您可能希望将文件扩展名存储在数据库中,并附上有关该图像的其他详细信息: - )

答案 1 :(得分:4)

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

您可以从下面的列表中更改项目名称

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)

这可能会对你有帮助。

答案 2 :(得分:-1)

上传文件后,您可以通过以下方式获取其财产:

$saved_file_name = $this->upload->data('file_name');
// will give you the filename along with the extension

如果您想在上传之前只获取文件扩展名,请使用核心PHP:

$file_ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);

或使其干净

$filename= $_FILES["file"]["name"];
$file_ext = pathinfo($filename,PATHINFO_EXTENSION);