简体   繁体   English

查找具有特定名称但文件扩展名可变的文件

[英]Find files with certain name but variable file extension

I'd like to be able to select a file by just giving it's name (without extension). 我希望仅通过提供文件名即可选择文件(不带扩展名)。 For example, I might have a variable $id holding 12 . 例如,我可能有一个变量$id持有12 I want to be able to select a file called the-id-in-the-variable, say, 12.png from a directory, but it may have any one of a number of file extensions, listed below: 我希望能够从目录中选择一个名为the-id-in-the-variable的文件,例如12.png ,但它可能具有以下列出的许多文件扩展名中的任何一个:

  • .swf .swf
  • .png .png
  • .gif .gif
  • .jpg .jpg

There is only one occurrence of each ID. 每个ID仅出现一次。 I could use a loop and file_exists() , but is there a better way? 我可以使用循环和file_exists() ,但是有更好的方法吗?

Thanks, 谢谢,

James 詹姆士

$matches = glob("12.*");

would return an array with all the matching filenames in the current directory. 将返回一个包含当前目录中所有匹配文件名的数组。 glob() works much the same as wildcard matching at the shell prompt. glob()工作原理与shell提示符下的通配符匹配相同。

Take a look at glob . 看看glob Unfortunately, the exact semantics of the $pattern parameter is not described in the manual. 不幸的是,手册中没有描述$pattern参数的确切语义。 But it seems your problem can be solved using this function. 但是看来您的问题可以使用此功能解决。

Quick question to OP here: 在这里向OP快速提问:

What is the file extension of this file: somefile.tar.gz ? 该文件的文件扩展名是somefile.tar.gz Is it .gz or .tar.gz ? .gz还是.tar.gz :) I ask because most would answer this question as .tar.gz ... :)我问,因为大多数人都会以.tar.gz形式回答这个问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM