PHP检查文件存在而不知道扩展名

时间:2022-11-24 14:31:42

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 ?

没有做循环的任何想法的方法吗?

3 个解决方案

#1


49  

You would have to do a glob():

你必须做一个glob():

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

and see whether $result contains anything.

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

#2


3  

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).

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

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

注意: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

希望这可以帮助某人,伊万

#3


-1  

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

#1


49  

You would have to do a glob():

你必须做一个glob():

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

and see whether $result contains anything.

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

#2


3  

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).

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

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

注意: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

希望这可以帮助某人,伊万

#3


-1  

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