Jump to content

Recommended Posts

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>");

}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/160774-solved-insert-image-based-on-file-type/
Share on other sites

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;
?>

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

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>");

}
}
?>

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>");

}
?>

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

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>");
	}
}
}
?>

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>");
      }
   }
}
?>

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

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:

th.acedd761e6.jpg

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.