divadiva Posted January 23, 2009 Share Posted January 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/ Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744337 Share on other sites More sharing options...
flyhoney Posted January 23, 2009 Share Posted January 23, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744340 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744347 Share on other sites More sharing options...
flyhoney Posted January 23, 2009 Share Posted January 23, 2009 Are all of the extensions in the database '*.JPG' and all of the images in the filesystem '*.jpg' ? Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744356 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 Thanks for replying again. In database I have "jpg" whereas on server (around 400) image files are JPG. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744359 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744364 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744374 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744385 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 How to do that?Any idea?I cannot think of anything like that. Best Divya Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744389 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744394 Share on other sites More sharing options...
flyhoney Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744397 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744400 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 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)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744414 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744446 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 Experts, Waiting for replies. Best Regards, Divya Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744469 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744480 Share on other sites More sharing options...
divadiva Posted January 23, 2009 Author Share Posted January 23, 2009 When I try to run it : <?php $files = glob('*.JPG'); foreach($files as $file){ rename($file,strtolower($file)); } ?> I get a blank page.The file name doesnot change. Can someone help me out ? Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744546 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 That minimal amount of code works on the files in the folder it is in. Quote Link to comment https://forums.phpfreaks.com/topic/142115-solved-how-to-handle-image-extensionsjpgjpg/#findComment-744620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.