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


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

例如 :

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


同样在这里 :

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

有些人使用pathinfo($file)和/或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);


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

same here :

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

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


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的文件扩展名. 。 但是没有理智的人会像这样引入文件扩展名,因为它会在任何地方破坏加上它会打破隐含的约定。

例如在文件中 - 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 - tar.gz would be the extention..

Nope, it's much simpler - and maybe that is the root of your worries. The extension of 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.