php更好的处理文件扩展名的方法[重复](php better way to handle file extension [duplicate])

可能重复: 如何在PHP中提取文件扩展名? 获取文件扩展名(basename?)

尝试从其他人的代码中学习,我看到很多方法从它的扩展中剥离文件名,但是大多数方法看起来太局部化,因为它们假设某种条件。 例如 :

这将假设只有3个字符的扩展名(如.txt , .jpg , .pdf )

substr($fileName, 0, -4);

要么

substr($fileName, 0, strrpos($fileName, '.'));

但这可能会导致.jpeg , .tiff .html等文件名出现问题。 或者只有2个像.js或.pl

(浏览此列表显示一些文件名只能有1个字符,有些则多达10个(!))

我看到的一些其他方法依赖于点( . )

例如 :

return key(explode(“.”, $filename));

可能导致文件名出现问题,如20121029.my.file.name.txt.jpg

同样在这里 :

return preg_replace('/\.[^.]*$/', '', $filename);

有些人使用pathinfo($file)和/或basename() (它总是安全吗??)

basename($filename);

和许多其他方法..

所以我的问题有几个部分:

“剥离”文件扩展名的最佳方法是什么? (有点)

什么是“获取”文件扩展名(没有点)和/或检查它的最佳方法

php自己的函数(basename)会识别所有扩展,无论它们有多么奇特或文件名是如何构造的?

如果操作系统对此事有任何影响,该怎么办? (win,linux,unix ......)

我希望得到答案的所有那些小的子问题可以总结为一个单一的问题: 是否有防弹,整体,始终工作,防止失败,最佳实践,über_function将在所有和任何条件下工作?

编辑I - 另一个文件扩展名列表

Possible Duplicate: How to extract a file extension in PHP? Get the file extension (basename?)

trying tot learn from other people´s code , I see a lot of methods to strip a filename from it´s extension, but most of the methods seems too localized as they assume a certain condition. for example :

This will assume only 3-character extension (like .txt, .jpg, .pdf)

substr($fileName, 0, -4);

or

substr($fileName, 0, strrpos($fileName, '.'));

But this can cause problems on file names like .jpeg, .tiff .html . or only 2 like .jsOr .pl

(browsing this list shows some file names can have only 1 character, and some as many as 10 (!) )

some other methods i have seen rely on the point (.)

for example :

return key(explode(“.”, $filename));

Can cause problems with filenames like 20121029.my.file.name.txt.jpg

same here :

return preg_replace('/\.[^.]*$/', '', $filename);

some people use the pathinfo($file) and / or basename() (is it ALWAYS safe ?? )

basename($filename);

and many many other methods ..

so my question has several parts :

what is the best way to "strip" a file extension ? (with the point)

what is the best way to "get" the file extension (without the point) and / or check it

will php own functions (basename) will recognize ALL extensions regardless of how exotic they might be or how the filename is constructed ?

what if any influence does the OS has on the matter ? (win, linux, unix...)

all those small sub-questions , which i would like to have an answer to can be summed-up in an overall single question : Is there a bullet-proof , overall, always-work, fail-proof , best-practice , über_function that will work under all and any condition ??

EDIT I - another file extension list

最满意答案

引用重复问题的最佳答案:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

这是最好的方式。 它由操作系统提供,您可以做到最好。 我知道没有不起作用的情况。

一个例外是包含a的文件扩展名. 。 但是没有理智的人会像这样引入文件扩展名,因为它会在任何地方破坏加上它会打破隐含的约定。

例如在文件20121021.my.file.name.txt.tar.gz中 - tar.gz将是扩展..

不,它更简单 - 也许这是你担忧的根源。 20121021.my.file.name.txt.tar.gz的扩展名为.gz 。 它是一个gzip压缩的.gz文件,用于所有意图和目的。 只有当你解压缩它时,它才会成为.tar文件。 在此之前,文件名中的.tar没有意义,仅用作gunzip工具的信息。 没有名为.tar.gz文件扩展名。

也就是说,检测文件扩展名无法帮助您确定文件是否实际上是它声称的类型。 但我相信你知道这一点,只是把这个放在未来的读者身上。

Quoting from the duplicate question's top answer:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

this is the best available way to go. It's provided by the operating system, and the best you can do. I know of no cases where it doesn't work.

One exception would be a file extension that contains a .. But no sane person would introduce a file extension like that, because it would break everywhere plus it would break the implicit convention.

for example in a file 20121021.my.file.name.txt.tar.gz - tar.gz would be the extention..

Nope, it's much simpler - and maybe that is the root of your worries. The extension of 20121021.my.file.name.txt.tar.gz is .gz. It is a gzipped .gz file for all intents and purposes. Only when you unzip it, it becomes a .tar file. Until then, the .tar in the file name is meaningless and serves only as information for the gunzip tool. There is no file extension named .tar.gz.

That said, detecting the file extension will not help you determine whether a file is actually of the type it claims. But I'm sure you know that, just putting this here for future readers.

更多推荐