Is it possible to get a file extension without knowing it?

后端 未结 4 1289
礼貌的吻别
礼貌的吻别 2021-02-14 14:52

I have a file called 94bf663a100e848fb599209af8cdc2b5.wmv. I know pathinfo will not give me the extension if I just use the name 94bf663a100e848fb599209af8cdc2b5. I

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-14 15:10

    As the example on the php glob manual page suggests, glob does not simply check if the file exists, it returns every file that matches the expression.

    Here's a modification of the example on that page for your needs:

    $name = "94bf663a100e848fb599209af8cdc2b5";
    $matching = glob($name . ".*");
    
    $info = pathinfo($matching[0]);
    $ext = $info['extension'];
    

    This assumes there is one (and only one) file with that name (with any extension), but you should be able to modify it if the file might not exist, or there might be multiple files with the same name, and different extensions.

提交回复
热议问题