Jump to content

Recommended Posts

Experts,

 

In the database I have images with jpg extension  where as they are stored as JPG on the server(where my php pages are).

For example :

In mysql database I have ; Image 1 = abc.jpg

On the server where my php pages are I have = abc.JPG

 

The picture will not be displayed.

 

Whereas if I have abc.jpg and abc.jpg in database and Server .It will work.

 

How to handle image extensions?

 

Here is the code for viewing images:

 

<TR> 
<TD WIDTH="180" STYLE="background-color:#B6DAF2;"><B>Image 1</B></TD>
  <TD><% if($i_row['image1'] && ! substr_count($i_row['image1'],'unavailable.')) { %><IMG
BORDER="0" SRC="<%= '../files/' . $i_row['image1']; %>" ALT=" " WIDTH="300"><% } %></TD> 
//Files is where images are stored on server.$i_row[image1] is coming from database.
</TR> 
<TR>

 

 

Thanks in advance.

 

Best Regards,

Divya

Do you upload these images? Why not strtolower the image file names, like you do the DB?

 

The only other way to work around this is have all possible cases in an array and loop through with file_exists. Which I highly suggest against. Put the right file name in the DB and you will be fine.

 

EDIT:

And given that you are using the ASP tags, I will take it that you are used to working on a windows system and have switched to a *nix system. If this is the case that is problem, *nix is case SenSitIve where as windows is case insensitive.

Use this function to remove the file extension from the image:

 

function strip_ext($filename)
{
if (($pos = strrpos($filename, '.')) !== false)
{
	return substr($filename, 0, $pos);
} 
else 
{
	return $filename;
} 
}

 

So your code would be like this:

 

<TR>
<TD WIDTH="180" STYLE="background-color:#B6DAF2;"><B>Image 1</B></TD>
  <TD><% if($i_row['image1'] && ! substr_count($i_row['image1'],'unavailable.')) { %><IMG
BORDER="0" SRC="<%= '../files/' . strip_ext($i_row['image1']) . ".jpg"; %>" ALT=" " WIDTH="300"><% } %></TD>
//Files is where images are stored on server.$i_row[image1] is coming from database.
</TR>
<TR>

Thankyou for replying.

I have to do some maual upload for some products.

 

I have a question what should be the "FILENAME"?As I have quite a lot of image files.Or should I specify the folder?


function strip_ext($filename)
{
if (($pos = strrpos($filename, '.')) !== false)
{
	return substr($filename, 0, $pos);
} 
else 
{
	return $filename;
} 
}


 

Best Regards,

Divya

 

 

 

If you have mis-match of file name/extension letter-case between the actual files and data in your database, I recommend renaming the files to be all lower case and converting the data in the database to be the same as the actual file names.

That option will be time consumeing as data is already loaded.I would have to manually delete files in database as well server.

 

Best

Divya

 

You can use PHP to rename them on the server to be lowercase and have this done for you, shouldn't take too long to do it.

How to do that?Any idea?I cannot think of anything like that.

 

Best

Divya

 

rename

 

Just loop through your database and invoke that rename from .JPG to .jpg so use the file_exists on the cap JPG if that exists rename it, else keep looping.

Use this function to remove the file extension from the image:

 

function strip_ext($filename)
{
if (($pos = strrpos($filename, '.')) !== false)
{
	return substr($filename, 0, $pos);
} 
else 
{
	return $filename;
} 
}

 

So your code would be like this:

 

<TR>
<TD WIDTH="180" STYLE="background-color:#B6DAF2;"><B>Image 1</B></TD>
  <TD><% if($i_row['image1'] && ! substr_count($i_row['image1'],'unavailable.')) { %><IMG
BORDER="0" SRC="<%= '../files/' . strip_ext($i_row['image1']) . ".jpg"; %>" ALT=" " WIDTH="300"><% } %></TD>
//Files is where images are stored on server.$i_row[image1] is coming from database.
</TR>
<TR>

 

As long as your database extensions are all 'JPG' and your file extensions are all 'jpg', than my solution above works just fine.  If you notice when you output the img src, I am calling my strip_ext() function which removes the extension from the file name.  Then, I append '.jpg' to the end of the file name.  This should convert the uppercase extensions in your database to lowercase.

Flyhoney ,

 

I am not sure about FILENAME as I have number of image files.Can you clarify if it is  name of the folder or images?

Also,in database I have jpg and  server JPG.But I can fix that.

 

Thanks in advance

 

 

Best Regards,

Divya

The following will rename all the .JPG files in a folder to all lower case (assuming that you don't have any upper-case letters in the file names that you want or need to keep) -

 

<?php
$files = glob('*.JPG');
foreach($files as $file){
rename($file,strtolower($file));
}
?>

PFMaBiSmAd,

 

Thanks for replying

I am confused so, as  where to put this code?

<?php
$files = glob('*.JPG');
foreach($files as $file){
rename($file,strtolower($file));
}
?>

 

 

WHERE AS My view image file has:

 


<TR> 
  <TD WIDTH="180" STYLE="background-color:#B6DAF2;"><B>Image 1</B></TD>
  <TD><% if($i_row['image1'] && ! substr_count($i_row['image1'],'unavailable.')) { %><IMG
BORDER="0" SRC="<%= '../files/' . $i_row['image1']; %>" ALT=" " WIDTH="300"><% } %></TD> 
</TR> 
<TR> 
<TD WIDTH="180" STYLE="background-color:#B6DAF2;"><B>Image 2</B></TD>
  <TD><% if($i_row['image2'] && ! substr_count($i_row['image2'],'unavailable.')) { %><IMG
BORDER="0" SRC="<%= '../files/' . $i_row['image2']; %>" ALT=" " WIDTH="300"><% } %></TD> 
</TR> 
<TR> 
<TD WIDTH="180" STYLE="background-color:#B6DAF2;"><B>Image 3</B></TD>
	  
					  <TD><% if($i_row['image3'] && ! substr_count($i_row['image3'],'unavailable.')) { %><IMG
						 BORDER="0" SRC="<%= '../files/' . $i_row['image3']; %>" ALT=" " WIDTH="300"><% } %></TD> 
					</TR> 
					<TR>
					<TR> 
					  <TD WIDTH="180" STYLE="background-color:#B6DAF2;"><B>Image 4</B></TD>
					  
					  <TD><% if($i_row['image4'] && ! substr_count($i_row['image4'],'unavailable.')) { %><IMG
						 BORDER="0" SRC="<%= '../files/' . $i_row['image4']; %>" ALT=" " WIDTH="300"><% } %></TD> 
					</TR>  


 

 

Thankyou in advance.

 

Best Regards,

Divya

 

<?php
$files = glob('*.JPG');
foreach($files as $file){
   rename($file,strtolower($file));
}
?>

 

This would be a 1 time deal. It will make all images the lowercase .jpg.

 

You will need to modify where the file is being uploaded and make sure it saves the file as all lowercase also. Basically save that in a .php in your images directory and run it. It should rename all images to be .jpg and consistent.

 

Or, as flyhoney explained, if the file names are the same case, you can just pull out the filename, then remove that extension from it and use that. Although consistency between the DB and the server file system would be the ideal/best way, imo.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.