0

I'm trying to fix a picture display issue and I don't know how to fix it. When I uploaded the files on to my computer some files had extension .jpg and others .JPG. I rename all the pictures and made sure all extensions were .jpg.

Everything seemed to work fine until I created a file path in my web project. I use <img class="img" alt="" src="${initParam.productGalleryImagePath}${product.id} (1).jpg" />.

All the pictures that were originally .jpg upload fine but all the picture that were originally .JPG and converted to .jpg won't upload if I use ${initParam.productGalleryImagePath}${product.id} (1).jpg they will only upload if I use ${initParam.productGalleryImagePath}${product.id} (1).JPG as if changing them to .jpg did not take effect... But with the last statement all the original .jpg wont upload.

So it's either .jpg to display the .jpg or .JPG to display the .JPG. My IDE is apparently case sensitive. My fear is when I will upload my project to my host... How do I fix this?

user45678
  • 347
  • 4
  • 18

4 Answers4

1

Case sensitivity is something that you must pay attention to. Technically the files are the same but programmatically case differences will break your code as you have discovered.

To be safe, its good you check the extension casing and convert all to a fixed form (say .jpg or .JPG) before you process them further.

Example of such a function is strtolower() in PHP.

Update Example of code to check if a file is actually an image;

function is_image($path)
{
    $a = getimagesize($path);
    $image_type = $a[2];

    if(in_array($image_type, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP)))
    {
        return true;
    }

    return false;
}

Update2 : Here's a java code chuck link from SO -> Test if file is an image

Community
  • 1
  • 1
MarcoZen
  • 1,140
  • 14
  • 25
1

Renaming files only to change case can cause all sorts of trouble. It's even worse when you're dealing with source control systems. And worse again when sharing code between users on case-sensitive systems and those on case-insensitive systems. Not helped by things like Tomcat being case sensitive, even when running on a case-insensitive filesystem.

I wouldn't take a chance. Some potential steps:

  • copy the entire project to a new location and open it afresh - see if redeploying from here helps
  • move the jpg files into a new directory, or change something else in their names (just one character would be enough) - the idea being to foil any weird name caching issues
  • similar idea: rename them all to .jpeg

And in future, if you're adjusting the case of filenames, try to do it before adding them to your project.

CupawnTae
  • 13,144
  • 2
  • 25
  • 59
1

it's either .jpg to display the .jpg or .JPG to display the .JPG

.jpg & .JPG represent the same thing (an image in joint photo group format), the difference is in how those files are referenced. Unix based systems (Linux/Mac/etc.) are case sensitive, '.jpg' != '.JPG' while Windows ignores case when looking up files, '.jpg' == '.JPG'.

Standardize your file format or update your upload script to accept any case file extension- lower case is best for most conventions.

adam_bear
  • 372
  • 3
  • 7
  • Mac filesystems are not case-sensitive by default. And on the other side of the coin, Tomcat on Windows *is* case-sensitive. – CupawnTae May 04 '15 at 20:01
  • @adam_bear: How do I update my upload script to accept any case file extension? Do you mean within `web.xml`? Any good example? `:)` – user45678 May 04 '15 at 20:59
  • It could be in the server's `.haccess`, but more likely there is a restriction on allowed filetypes in the .jsp. `grep JPG *.jsp`would be an easy way to find it. – adam_bear May 05 '15 at 06:37
0

There's no difference as far as the file format goes, they are exactly the same. The only difference comes when referencing the said file.
For instance, if you were trying to access image.JPG and linked it with src="image.jpg" you will most likely get a broken link.

Only difference = When referencing.

Toby Cannon
  • 657
  • 5
  • 15
  • That's what I thought. But All the pictures have been renamed to extension `.jpg` but those who were originally with extension `.JPG` wont display... They will display only if I reference as `.JPG`. So are you saying that I should ignore the problem and upload the project to my host without fear? – user45678 May 04 '15 at 19:42
  • 1
    @user45678 - first check if those .JPG are valid jpeg files. Open them with a image viewer for an "eye-test". Better to be safe than sorry. – MarcoZen May 04 '15 at 19:48
  • @MarcoZen: After doing some reseach yesterday, I came accross what it is referred to as "Magic Number" to verify the validity of an image file. Although my mind did not grasp all of it, I think I got the gist of it. If I can understand the subject every file has a serie of characters specific to the system. JPEG image files begin with FF D8 and end with FF D9 for example. – user45678 May 05 '15 at 07:01
  • @MarcoZen: With that knowledge I opened the pictures orginally with .JPG in notepad and discovered the first letters were not the ones expected but these: `MM ` which are the letters for `.TIFF`. I might be wrong but with that deduction I think all .JPG files aren't actually JPEG in the first place... Now how am I supposed to fix this? With a file converter or something? Or can I just change the code of the file to match a JPEG [link](http://en.wikipedia.org/wiki/Magic_number_%28programming%29#Magic_numbers_in_files )? – user45678 May 05 '15 at 07:07
  • @user45678 - where are you getting these jpegs from ? Its better you fix the REAL problem ... – MarcoZen May 05 '15 at 08:59
  • @user45678 - i have updated my answer to include some code to check if a file is a real image... Pls look for the java equivalent. – MarcoZen May 05 '15 at 09:01
  • @MarcoZen: Thanks for your answer. I'm sorry I never did that before, where am I supposed to paste the code in my project? `:/` – user45678 May 05 '15 at 19:15
  • @user45678 - use pastebin and stick your code there ? or stick that relevant part here ? – MarcoZen May 06 '15 at 14:06