关于php取得文件扩展名的问题

WBOY
Release: 2016-06-06 20:08:19
Original
966 people have browsed it

一般情况下,取得类似这样 test.txt, test.html, test.png 等扩展名没什么问题。

出现这样子,比如:test.png.txt ,问题也不大,因为我们可以认为.png是文件名而不是扩展名

但是,如果是 test.tar.gz 这种应该怎么弄?白名单?

回复内容:

一般情况下,取得类似这样 test.txt, test.html, test.png 等扩展名没什么问题。

出现这样子,比如:test.png.txt ,问题也不大,因为我们可以认为.png是文件名而不是扩展名

但是,如果是 test.tar.gz 这种应该怎么弄?白名单?

文件名后缀应该规范 test.tar.gz .tar是打包文件 .gz是压缩文件
linux压缩文件非常规范
test.tar.gz gzip压缩的 tar打包
test.gz gzip压缩的,没有打包
test.tar.bz2 bunzip2压缩的 tar打包
test.bz2 bunzip2压缩的 没有打包

在编程世界中只有对底层操作进行约束才可以实现优美的上层代码。 如果某个文件是用%做为文件后缀的
如果我的这接口会这么设计

class fileInfo(){ public function reFileName(); #文件名 public function getRealSuffix(); #最后一个 test.tar.gz 返回gz public function getFullSuffix(); #全部后缀test.tar.gz 返回tar.gz public function getSuffixs(); #后缀名数组 }
Copy after login

可以限制文件格式,通过已知后缀匹配

扩展名可以这样获取

$info = pathinfo($file); echo $info['extension'];
Copy after login

其实你是想做文件类型过滤吧,建议用mimetype而不是扩展名,更可靠一些。
可以参考 http://php.net/manual/zh/book.fileinfo.php 从php5.3开始可以直接使用了。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!