更好的方法来查看文件是否具有某些扩展名

I'm doing this right now in my code to see if a file name has the extension .txt, but I think it basically checks if it contains .txt and not necessarily ends with .txt. Does php have a better way to do extension checking instead of using strpos?

strpos($filename,'.txt') !== false

You can allow multiple extensions by adding to the array.

$allowed = array('txt');

if (in_array(pathinfo($filename, PATHINFO_EXTENSION), $allowed)){
    // Has the correct file extension
}

or a simpler version which allows only txt extensions could be:

if (pathinfo($filename, PATHINFO_EXTENSION) == 'txt'){
    // Has the correct file extension
}

You can use the pathinfo() function to get the extension of the file:

$info = pathinfo($pathToFile);
$ext = $info['extension'];

and then check if the extension is one of the allowed:

$validExtensions = array("txt", "doc");
if (in_array($ext, $validExtensions) {
    //more code
}
$ext = pathinfo($filename, PATHINFO_EXTENSION);

Use the following code, it couldn't get any better:

$ext = pathinfo('test.txt', PATHINFO_EXTENSION);

strpos is okay, you have to just keep in mind that you need not first but last dot.
and use strrpos(), note the double "r".