Ty44ler Posted June 3, 2009 Share Posted June 3, 2009 I am calling a list of files from a folder and I'd like to display a little icon of the file type next to it in the list. Also if it is calling a sub-folder I'd like it to display an image of a folder. How would I go about doing this? There are about 12 different file types. Would I have to run 12 different for loops? That seems inefficient. I'm really new to this so explaining the answer to me as if you were explaining it to a 5 year old would be great. Thank you in advance! Here's the code where I call everything within a folder... <?php while(false != ($file = readdir($dir))) { if($file!="." && $file!=".." && $file!="FTPCss.css" && $file!="images" && $file!="index.php") { echo(" <p><a href=\"$file\">$file </a></p>"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/ Share on other sites More sharing options...
MadTechie Posted June 3, 2009 Share Posted June 3, 2009 heres 2 options <?php //loop $ext = strtolower(array_pop(explode('.',$filename))); switch($ext) { case "txt": $icon = "notepad.gif" break; case "php": $icon = "script.gif" break; case "html": case "htm": $icon = "ff.gif" break; } echo $icon; ?> option #2 <?php $icons = array( 'txt' => 'notepad.gif', 'htm' => 'ff.gif', 'html' => 'ff.gif', 'php' => 'script.gif'); //loop $ext = strtolower(array_pop(explode('.',$filename))); $icon =(isset($icons[$ext]))?$icons[$ext]:"none.gif"; echo $icon; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848508 Share on other sites More sharing options...
kickstart Posted June 3, 2009 Share Posted June 3, 2009 Hi I would probably use something like this (probably a typo in it):- <?php $fileImage = array('.jpg' => 'JpegImage.jpg','.png' => 'PngImage.jpg','.pdf' => 'PdfImage.jpg','.xls' => 'ExcelImage.jpg'); while(false != ($file = readdir($dir))) { if($file!="." && $file!=".." && $file!="FTPCss.css" && $file!="images" && $file!="index.php") { echo(" <p><a href=\"$file\">$file </a><img src='".$fileImage[strtolower(substr($file,strrpos($file'.')))]."' /></p>"); } } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848518 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Hi I would probably use something like this (probably a typo in it):- <?php $fileImage = array('.jpg' => 'JpegImage.jpg','.png' => 'PngImage.jpg','.pdf' => 'PdfImage.jpg','.xls' => 'ExcelImage.jpg'); while(false != ($file = readdir($dir))) { if($file!="." && $file!=".." && $file!="FTPCss.css" && $file!="images" && $file!="index.php") { echo(" <p><a href=\"$file\">$file </a><img src='".$fileImage[strtolower(substr($file,strrpos($file'.')))]."' /></p>"); } } ?> All the best Keith It's coming up with a parse error. from what I can tell everything seems correct though... Here's the code... <?php $fileImage = array('.jpeg' => 'jpeg_icon.jpeg','.pdf' => 'Adobe_PDF_icon.png'); while(false != ($file = readdir($dir))) { if($file!="." && $file!=".." && $file!="FTPCss.css" && $file!="images" && $file!="index.php" && $file!="Thumbs.db") { echo(" <p><a href=\"$file\">$file </a><img src='".$fileImage[strtolower(substr($file,strrpos($file'.')))]."' /></p>"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848543 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 So, I figured it out. It was a small comma.... (substr($file,strrpos($file, '.')) Thank you for your help!!!! Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848582 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 The code works great, but there are some stupidly named files with dots in the middle of them and images aren't showing up for those kind. How do you get around this? After looking it up, it seems like using an explode function, but I don't know how to implement it... Also, I'm trying to detect whether its a folder or not and display a folder icon. I was using is dir() function but just like the explode, Im not having much luck. <?php $fileImage = array('.pdf' => 'images/Adobe_PDF_icon.png', '.jpg' => 'images/jpeg_icon.jpeg'); while(false != ($file = readdir($dir))) { if($file!="." && $file!=".." && $file!="FTPCss.css" && $file!="images" && $file!="index.php" && $file!="Thumbs.db") { echo("<p><a href=\"$file\"> <img src='".$fileImage[strtolower(substr($file, strpos($file, '.')))]."' /> $file</a> </p>"); } if(is_dir($file) echo ("<p><a href=\"$file\"> <img src='images/jpeg_icon.jpeg' /> $file</a> </p>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848671 Share on other sites More sharing options...
MadTechie Posted June 3, 2009 Share Posted June 3, 2009 use is_dir() Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848681 Share on other sites More sharing options...
kickstart Posted June 3, 2009 Share Posted June 3, 2009 Hi Bit of a tidy up for the rest (use the is_dir() suggestion above as well). <?php $fileImage = array('pdf' => 'images/Adobe_PDF_icon.png', 'jpg' => 'images/jpeg_icon.jpeg'); while(false != ($file = readdir($dir))) { if($file!="." && $file!=".." && $file!="FTPCss.css" && $file!="images" && $file!="index.php" && $file!="Thumbs.db") { $FileParts = explode(',',$file); $FileExtension = strtolower($FileParts[count($FileParts) - 1]); echo("<p><a href=\"$file\">".((in_array($FileExtension,$fileImage)) ? "<img src='".$fileImage[$FileExtension]."' />" : "")." $file</a> </p>"); } if(is_dir($file) echo ("<p><a href=\"$file\"> <img src='images/jpeg_icon.jpeg' /> $file</a> </p>"); } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848683 Share on other sites More sharing options...
MadTechie Posted June 3, 2009 Share Posted June 3, 2009 I made a few A slight change to kickstarts code including unknown.gif for files without a known type, and irgnore list (don't show files) <?php $ignore = array(".","..","FTPCss.css","images","Thumbs.db"); $fileImage = array( 'pdf' => 'images/Adobe_PDF_icon.png', 'jpg' => 'images/jpeg_icon.jpeg'); while(false != ($file = readdir($dir))){ if(!in_array($file,$ignore)){ if(is_dir($file)) { echo ("<p><a href=\"$file\"> <img src='images/jpeg_icon.jpeg' /> $file</a> </p>"); }else{ $FileParts = explode(',',$file); $FileExtension = strtolower($FileParts[count($FileParts) - 1]); echo("<p><a href=\"$file\">".(isset($fileImage[$FileExtension]) ? "<img src='".$fileImage[$FileExtension]."' />" : "<img src='unknown.gif' />")." $file</a> </p>"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848687 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 I appreciate the quick responses. No picture is showing up next to the listings though. Could this be an issue with my image folder or how I named them? ??? <?php $ignore = array(".","..","FTPCss.css","images","Thumbs.db"); $fileImage = array( 'pdf' => 'images/Adobe_PDF_icon.png', 'jpg' => 'images/jpeg_icon.jpeg'); while(false != ($file = readdir($dir))){ if(!in_array($file,$ignore)){ if(is_dir($file)) { echo ("<p><a href=\"$file\"> <img src='images/folder.jpeg' />$file</a> </p>"); }else{ $FileParts = explode(',',$file); $FileExtension = strtolower($FileParts[count($FileParts) - 1]); echo("<p><a href=\"$file\">".(isset($fileImage[$FileExtension]) ? "<img src='".$fileImage[$FileExtension]."' />" : "<img src='unknown.gif' />")." $file</a> </p>"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848695 Share on other sites More sharing options...
kickstart Posted June 3, 2009 Share Posted June 3, 2009 Hi Nothing obvious. It is looking for the image file in an image subdirectory of the scripts directory. Also check the generated page to check if the img tag has been set up correctly. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848748 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Thanks for the tip, that helped... I just checked the output code... all the images are coming up as unknown except for the folder which shows folder.jpeg but its still not showing up either... hmmm... Why is that? Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848754 Share on other sites More sharing options...
kickstart Posted June 3, 2009 Share Posted June 3, 2009 Hi Spotted one thing. Typo that is my fault. $FileParts = explode(',',$file); should be $FileParts = explode('.',$file);. As a quick check, specify the full url for the images rather that just the relative ones. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848774 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 ok sorry, I'm probably responding a lot to my own questions here, but I figured out the problem for the folder, that displays fine, however the pdf files and jpg files are still being treated as unknowns.... Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848776 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 made the correction... But these dang files are still being seen as unknown. This is getting frustrating. Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848783 Share on other sites More sharing options...
MadTechie Posted June 3, 2009 Share Posted June 3, 2009 Make sure your $fileImage array keys are lowercase $fileImage = array( 'pdf' => 'images/Adobe_PDF_icon.png', 'jpg' => 'images/jpeg_icon.jpeg'); is ok, but $fileImage = array( 'Pdf' => 'images/Adobe_PDF_icon.png', 'Jpg' => 'images/jpeg_icon.jpeg'); will fail if that doesn't work can you post what you have Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848797 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Here is what I have now... folder image works fine and the unknown displays the right image, but it displays it for all of them. <?php $ignore = array(".","..","FTPCss.css","images", "Thumbs.db", "index.php"); $fileImage = array( '.pdf' => 'images/Adobe_PDF_icon.png', '.jpg' => 'images/jpeg_icon.jpeg'); while(false != ($file = readdir($dir))){ if(!in_array($file,$ignore)){ if(is_dir($file)) { echo ("<p><a href=\"$file\"> <img src='images/folder.jpg' />$file</a> </p>"); }else{ $FileParts = explode('.', $file); $FileExtension = strtolower($FileParts[count($FileParts) -1]); echo("<p><a href=\"$file\">".(isset($fileImage[$FileExtension]) ? "<img src='".$fileImage[$FileExtension]."' />" : "<img src='images/file_icon.jpg'/>")." $file</a> </p>"); } } } ?> Here is my HTML output code: <p><a href="06-12-06 Site Phasing RFP 0715-A100.pdf"><img src='images/file_icon.jpg'/> 06-12-06 Site Phasing RFP 0715-A100.pdf</a> </p><p><a href="06-12-06 Site Phasing RFP C004.2008 0604.pdf"><img src='images/file_icon.jpg'/> 06-12-06 Site Phasing RFP C004.2008 0604.pdf</a> </p><p><a href="06-12-08 BARRACKS MECHANICAL CALCS.pdf"><img src='images/file_icon.jpg'/> 06-12-08 BARRACKS MECHANICAL CALCS.pdf</a> </p><p><a href="06-12-08 BARRACKS Mechanical M201.pdf"><img src='images/file_icon.jpg'/> 06-12-08 BARRACKS Mechanical M201.pdf</a> </p><p><a href="06-12-08 Battalion HQ Mechanical M202.pdf"><img src='images/file_icon.jpg'/> 06-12-08 Battalion HQ Mechanical M202.pdf</a> </p><p><a href="06-12-08 Mech. Plant Flow Diagrams M401.pdf"><img src='images/file_icon.jpg'/> 06-12-08 Mech. Plant Flow Diagrams M401.pdf</a> </p><p><a href="06-12-08 Mech. Plant Flow Diagrams M501.pdf"><img src='images/file_icon.jpg'/> 06-12-08 Mech. Plant Flow Diagrams M501.pdf</a> </p><p><a href="06-12-08 Mech. Plant Mechanical M202.pdf"><img src='images/file_icon.jpg'/> 06-12-08 Mech. Plant Mechanical M202.pdf</a> </p><p><a href="06-12-08 RFP C005 Drawing.pdf"><img src='images/file_icon.jpg'/> 06-12-08 RFP C005 Drawing.pdf</a> </p><p><a href="06-12-08 SITE Chilled, Hot Water M301.pdf"><img src='images/file_icon.jpg'/> 06-12-08 SITE Chilled, Hot Water M301.pdf</a> </p><p><a href="06-16-08 Mechanical Plant Calc Update.pdf"><img src='images/file_icon.jpg'/> 06-16-08 Mechanical Plant Calc Update.pdf</a> </p><p><a href="06-18-08 SITE Water and Sewer Update.pdf"><img src='images/file_icon.jpg'/> 06-18-08 SITE Water and Sewer Update.pdf</a> </p><p><a href="Ft Stewart - Winn Army Community Hospital"> <img src='images/folder.jpg' />Ft Stewart - Winn Army Community Hospital</a> </p><p><a href="OSF x 24.cal"><img src='images/file_icon.jpg'/> OSF x 24.cal</a> </p><p><a href="Parris Island Narratives Part1.pdf"><img src='images/file_icon.jpg'/> Parris Island Narratives Part1.pdf</a> </p> </div> Here's a screenshot: Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848806 Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 I got it! thank you all so much! that dot in front of the pdf and jpg had to go Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848813 Share on other sites More sharing options...
MadTechie Posted June 3, 2009 Share Posted June 3, 2009 Solved ? if so please click topic solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/#findComment-848849 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.