How to display an image if the file name in the folder is different from the file name in the database?

I am currently displaying a file name from a database in my PHP page. However, some file names in server folders have a different case. Thus, the database can say image1.jpg, and the file name on the server can say "image1.JPG" in uppercase. This is a random value for some files. These files are not displayed. Is there a way I can use a function so that it can be displayed. We are talking about more than 1000 files here. Therefore, any help would be greatly appreciated.

+5
source share
3 answers

I would run the user-defined function file_exists () to check in which case the image extension.

Use this custom function to check the correct case (skip it in lowercase, then use lowercase letters if it returns 1, or use uppercase letters if it returns 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
    }
}

You really have to go in and make all the file name extensions lowercase when you get the time.

How could you do this by running glob () through the directories: http://php.net/manual/en/function.glob.php and renaming each file extension to lowercase using strtolower (): http: // php .net / manual / en / function.strtolower.php

+5
source

, . , , , - :

find . -name '*.*' -exec sh -c '
a=$(echo {} | sed -r "s/([^.]*)\$/\L\1/");
[ "$a" != "{}" ] && mv "{}" "$a" ' \;
+1

Use file_existsto check. And expand this to compensate for the problems you are facing. I am using a function called replace_extension() shown here .

<?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;
}

?>
0
source

All Articles