PHP 检查文件是否存在,但不知道扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3303691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:17:02  来源:igfitidea点击:

PHP check file exists without knowing the extension

phpfile-extensionfile-exists

提问by Lee

I need to check if a file exists but I don't know the extension.

我需要检查文件是否存在,但我不知道扩展名。

IE I would like to do:

IE 我想做:

if(file_exists('./uploads/filename')):
 // do something
endif;

Of course that wont work as it has no extension. the extension will be either jpg, jpeg, png, gif

当然这行不通,因为它没有扩展名。扩展名将是 jpg、jpeg、png、gif

Any ideas of a way of doing this without doing a loop ?

任何不做循环的方法的想法?

回答by Pekka

You would have to do a glob():

你必须做一个 glob():

$result = glob ("./uploads/filename.*");

and see whether $resultcontains anything.

并查看是否$result包含任何内容。

回答by Ioan Chiriac

I've got the same need, and tried to use glob but this function seems to not be portable :

我有同样的需求,并尝试使用 glob 但此功能似乎不可移植:

See notes from http://php.net/manual/en/function.glob.php:

请参阅http://php.net/manual/en/function.glob.php 中的注释:

Note: This function isn't available on some systems (e.g. old Sun OS).

Note: The GLOB_BRACE flag is not available on some non GNU systems, like Solaris.

注意:此功能在某些系统上不可用(例如旧的 Sun 操作系统)。

注意: GLOB_BRACE 标志在某些非 GNU 系统上不可用,例如 Solaris。

It also more slower than opendir, take a look at : Which is faster: glob() or opendir()

它也比 opendir 慢,看看:哪个更快:glob() 或 opendir()

So I've made a snippet function that does the same thing :

因此,我制作了一个执行相同操作的代码段函数:

function resolve($name) {
    // reads informations over the path
    $info = pathinfo($name);
    if (!empty($info['extension'])) {
        // if the file already contains an extension returns it
        return $name;
    }
    $filename = $info['filename'];
    $len = strlen($filename);
    // open the folder
    $dh = opendir($info['dirname']);
    if (!$dh) {
        return false;
    }
    // scan each file in the folder
    while (($file = readdir($dh)) !== false) {
        if (strncmp($file, $filename, $len) === 0) {
            if (strlen($name) > $len) {
                // if name contains a directory part
                $name = substr($name, 0, strlen($name) - $len) . $file;
            } else {
                // if the name is at the path root
                $name = $file;
            }
            closedir($dh);
            return $name;
        }
    }
    // file not found
    closedir($dh);
    return false;
}

Usage :

用法 :

$file = resolve('/var/www/my-website/index');
echo $file; // will output /var/www/my-website/index.html (for example)

Hope that could helps someone, Ioan

希望可以帮助某人,Ioan

回答by gearsdigital

Did you know about the PHP filetype function? http://php.net/manual/en/function.filetype.php

你知道 PHP 文件类型函数吗? http://php.net/manual/en/function.filetype.php

Or otherwise is_file()http://www.php.net/manual/en/function.is-file.php

或者以其他方式is_file()http://www.php.net/manual/en/function.is-file.php