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

WBOY
Libérer: 2016-06-06 20:08:19
original
1000 Les gens l'ont consulté

一般情况下,取得类似这样 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压缩的 没有打包

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

<code>class fileInfo(){
   public function reFileName(); #文件名
   public function getRealSuffix();  #最后一个  test.tar.gz   返回gz
   public function getFullSuffix();    #全部后缀test.tar.gz   返回tar.gz
   public function getSuffixs();   #后缀名数组
}</code>
Copier après la connexion

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

扩展名可以这样获取

<code>$info = pathinfo($file);
echo $info['extension'];</code>
Copier après la connexion

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

Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!