5

我目前正在我的 PHP 页面上显示数据库中的文件名。但是,服务器文件夹上的某些文件名有不同的大小写。所以数据库可能会说image1.jpg,而服务器上的文件名可能会说“image1.JPG”大写。这对于某些文件是随机的。这些文件不会显示。有没有办法可以使用一个函数来显示它。我们在这里谈论超过 1000 个文件。因此,我们将不胜感激任何帮助。

4

3 回答 3

5

我会运行一个自定义的 file_exists() 函数来检查图像的扩展名是哪种情况。

使用此自定义函数检查大小写是否正确(将其传递为小写,如果返回 1,则使用小写,如果返回 2,则使用大写):

function file_exists_case($strUrl)
{
    $realPath = str_replace('\\','/',realpath($strUrl));

    if(file_exists($strUrl) && $realPath == $strUrl)
    {
        return 1;    //File exists, with correct case
    }
    elseif(file_exists($realPath))
    {
        return 2;    //File exists, but wrong case
    }
    else
    {
        return 0;    //File does not exist
    }
}

不过,当你有时间时,你真的应该进去把所有的文件扩展名都改成小写。

您可以通过以下目录运行 glob( ):http: //php.net/manual/en/function.glob.php 并使用 strtolower() 将每个文件扩展名重命名为小写:http:// php.net/manual/en/function.strtolower.php

于 2013-01-27T02:15:13.270 回答
1

不确定是否可以选择将扩展名转换为小写。但是,如果没有其他系统依赖于某些要大写的扩展,那么您可以运行如下内容:

find . -name '*.*' -exec sh -c '
a=$(echo {} | sed -r "s/([^.]*)\$/\L\1/");
[ "$a" != "{}" ] && mv "{}" "$a" ' \;
于 2013-01-27T04:35:15.490 回答
0

用来file_exists做检查。并将其扩展以弥补您面临的问题。我正在使用replace_extension() 这里显示的函数。

<?php

// Full path to the file.
$file_path = '/path/to/the/great.JPG';

// Call to the function.
echo check_if_image_exists($file_path, $file_ext_src);

// The function itself.
function check_if_image_exists ($file_path) {

    $file_ext_src = end(explode('.', $file_path));

    if (file_exists($file_path)) {
        return TRUE;
    }
    else {

        if (ctype_lower($file_ext_src)) {
            $file_ext_new = strtoupper($file_ext_src); // If lowercase make it uppercase.
        }
        else if (ctype_upper($file_ext_src)) {
            $file_ext_new = strtolower($file_ext_src); // If uppercase make it lowercase.
        }

        // Now create a new filepath with the new extension & check that.
        $file_path_new = replace_extension($file_path, $file_ext_new);
        if (file_exists($file_path_new)) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }
}

// Nice function taken from elsewhere.
function replace_extension($filename, $new_extension) {
    $info = pathinfo($filename);
    return $info['filename'] . '.' . $new_extension;
}

?>
于 2013-01-27T02:32:42.603 回答