Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 532
ABCO Consulting Avatar asked Jan 27 '13 02:01

ABCO Consulting


People also ask

How can we store image name in database?

You can store the full image in the Database table by converting it into the base64 format. You don't need to store image reference in the Database table e.g. name, path, and not require to store the image on your server. In PHP base64_encode() method is been used for base64 conversion.

How do you find the file name of an image?

Right-click that file to open a context menu and then select "Properties." The Properties dialog box opens on the computer. The file name of the picture in question is listed to the right of Name.

Can we give different names to a file?

yes, we compile a java file with a different name than the class, provided that there should not be any public class in that file. If there is any public class in file then in that case you have to give that name as file name. But if your class does not contain any public class then you can give any name to you class.

Does a file path include the file name?

Paths include the root, the filename, or both. That is, paths can be formed by adding either the root, filename, or both, to a directory.


2 Answers

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

like image 200
Leng Avatar answered Oct 17 '22 16:10

Leng


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" ' \;
like image 36
Lloyd Banks Avatar answered Oct 17 '22 14:10

Lloyd Banks