5

I am currently displaying the file name from the database on my PHP page. However, some file names on the server's folders have a different case. So the database may say image1.jpg and the file name on the server may say "image1.JPG" in upper case. This is random with some of the files. These files do not get displayed. Is there a way that I can use a function so that it can be displayed. We are talking about more than 1000 files here. So any help would be highly appreciated.

  • 1
    How did the files get uploaded, and how did they get into the database? Seems they were not entered into one of them correctly. "img.JPG" is not equal to "img.jpg" when it comes to a linux file structure. – Hydra IO Jan 27 '13 at 02:13
  • I’ve dealth with database & file inconsistency nightmares like that. Never pleasant. The result of sloppy development & sloppy systems management. I would recommend that some kind of consistent data entry system be devised. And then create some scheme—either through a server script or in the code—to go through the DB entries, compare to the file system & even the mess out. It’s the only valid solution. I added some PHP code for an answer as well to help check for extension inconsistency. – Giacomo1968 Jan 27 '13 at 02:36

3 Answers3

5

I would run a custom file_exists() function to check for which case the image's extension is.

Use this custom function to check for the correct case (pass it lowercase, then use lowercase if it returns a 1, or use uppercase if it returns a 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 should go in and make all your file name extensions lowercase when you get the time, though.

The way you would do that is by running a glob() through the directories: http://php.net/manual/en/function.glob.php and renaming every file extension to lowercase using strtolower(): http://php.net/manual/en/function.strtolower.php

Leng
  • 2,948
  • 2
  • 21
  • 30
  • I am not too clear on the benefit of this function. Looking at `str_replace('\\','/',realpath($strUrl));` Is that simply switching forward slashes for backslashes? So wouldn’t this logic simply check if a file exists on a Windows file path versus a Unix/Mac OS X file path? Where is the case checking happening? – Giacomo1968 Jan 27 '13 at 02:48
  • 1
    @JakeGould It's really simple. The realpath call gives you the file's actual name, which includes whether the extension is uppercase or lowercase. So if you compare the passed path to the realpath, and they're identical, then the case of the passed path matches the realpath. You'll have to do the regex to compare the passed path to the realpath if the realpath is returning backslashes instead of forward slashes. – Leng Jan 27 '13 at 20:01
  • Ooooh! I get it now. I never knew about `realpath` but this tip exposed me to it. Great work! But will say I was thrown off by the PHP `realpath` command you have in place and the `$realPath` camelcase variable you have set. Visually seems to similar. – Giacomo1968 Jan 27 '13 at 20:17
1

Not sure if converting the extensions to lowercase is an option. But if there are no other systems that depend on certain extensions to be capitalized then you could run something like this:

find . -name '*.*' -exec sh -c '
a=$(echo {} | sed -r "s/([^.]*)\$/\L\1/");
[ "$a" != "{}" ] && mv "{}" "$a" ' \;
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
  • This would solve the filesystem naming issues. But if the DB still has the incorrect extension that would have to be changed as well. – Giacomo1968 Jan 27 '13 at 22:43
  • @JakeGould I would imagine a quick update statement on the table should solve this problem – Lloyd Banks Jan 28 '13 at 02:31
0

Use file_exists to do a check. And expand that out to compensate for the issues you are facing. I am using the 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;
}

?>
Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103