/    Sign up×
Community /Pin to ProfileBookmark

Directory lister, image for each file type

Good day to all,
I’m using window as server and php 4.3.
I need to I dentify each file type by an images for each of them.

[code=php]
function files($resultf) {

$handlef=opendir(“.”);
while ($filef = readdir($handlef)) {
if ($filef == “.” || $filef == “..” ) { } else {
//just an example:
if(is_dir($filef)){
echo ” “;
} else {
echo “<tr><td bgcolor=’#cccc99′ class=’red’ align=’left’ width=100% onmouseover=”bgColor=’#666666′” style=”cursor:pointer” onmouseout=”bgColor=’#cccc99′”><table border=0 align=’left’ cellspacing=1 cellpading=0 width=100%>”;
echo “<tr><td width=90% bgcolor=’#cccc99′><a href=”$filef” target=’texte’ class=’black’><img src=’file.jpg’ border=0>$filef</a></td>”;
echo “<td width=10% bgcolor=’#cccc99′>”;

echo “</td>”;
echo “<td width=10% bgcolor=’#cccc99′><a href=’delete.php?dfile=$filef’ target=’texte’ class=’black’><img src=’delete.gif’ border=0 width=17></a></td>”;
echo “</tr>”;
echo “</table>”;

}
}

[/code]

I need someting like

[code=php]
$icon = ‘file.jpg’;
if ($filef==’BAT’){$icon = ‘bat.gif’;}
if ($filef==’BMP’){$icon = ‘img.gif’;}
if ($filef==’EXE’){$icon = ‘exe.gif’;}
if ($filef==’GIF’){$icon = ‘img.gif’;}
if ($filef==’JPG’){$icon = ‘img.gif’;}
if ($filef==’PNG’){$icon = ‘img.gif’;}
if ($filef==’TIF’){$icon = ‘img.gif’;}
if ($filef==’TIFF’){$icon = ‘img.gif’;}
if ($filef==’DOC’){$icon = ‘doc.gif’;}
if ($filef==’XLS’){$icon = ‘xls.gif’;}
if ($filef==’PPT’){$icon = ‘ppt.gif’;}
if ($filef==’PDF’){$icon = ‘pdf.gif’;}
if ($filef==’GZ’){$icon = ‘zip.gif’;}
if ($filef==’TAR’){$icon = ‘zip.gif’;}
if ($filef==’RAR’){$icon = ‘zip.gif’;}
if ($filef==’TXT’){$icon = ‘txt.gif’;}
if ($filef==’ZIP’){$icon = ‘zip.gif’;}
$filetype = ‘<img src=”$icon.”>’;

[/code]

Can you help me assamble this, I’m new to PHP.

Thanks in advance !
Take care !

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@shane_carrAug 10.2006 — [code=php]
$icon = 'file.jpg';

$myfile = "the_file_to_be_evaluated.png";

$var1 = explode(".", $myfile);
$var2 = strtoupper($var1[count($var1)-1]);

if ($var2=='BAT'){$icon = 'bat.gif';}
if ($var2=='BMP'){$icon = 'img.gif';}
if ($var2=='EXE'){$icon = 'exe.gif';}
if ($var2=='GIF'){$icon = 'img.gif';}
if ($var2=='JPG'){$icon = 'img.gif';}
if ($var2=='PNG'){$icon = 'img.gif';}
if ($var2=='TIF'){$icon = 'img.gif';}
if ($var2=='TIFF'){$icon = 'img.gif';}
if ($var2=='DOC'){$icon = 'doc.gif';}
if ($var2=='XLS'){$icon = 'xls.gif';}
if ($var2=='PPT'){$icon = 'ppt.gif';}
if ($var2=='PDF'){$icon = 'pdf.gif';}
if ($var2=='GZ'){$icon = 'zip.gif';}
if ($var2=='TAR'){$icon = 'zip.gif';}
if ($var2=='RAR'){$icon = 'zip.gif';}
if ($var2=='TXT'){$icon = 'txt.gif';}
if ($var2=='ZIP'){$icon = 'zip.gif';}

$filetype = '<img src="$icon.">';
[/code]
Copy linkTweet thisAlerts:
@pcthugAug 10.2006 — Shane, why not just make use of the pathinfo ['extension'] instead of inaccurately exploding the string? Also such a task is better handled with the switch statement. Finally your $filetype variable will always return
<i>
</i>&lt;img src="$icon."&gt;

regardless of the value of $icon because it is a single quoted string.

Try this script:[code=php]
$file = pathinfo($filef);

switch(strtoupper($file['extension']))
{
case 'BAT': $icon = 'bat.gif'; break;
case 'BMP': $icon = 'img.gif'; break;
case 'EXE': $icon = 'exe.gif'; break;
case 'GIF': $icon = 'img.gif'; break;
case 'JPG': $icon = 'img.gif'; break;
case 'PNG': $icon = 'img.gif'; break;
case 'TIF': $icon = 'img.gif'; break;
case 'TIFF': $icon = 'img.gif'; break;
case 'DOC': $icon = 'doc.gif'; break;
case 'XLS': $icon = 'xls.gif'; break;
case 'PPT': $icon = 'ppt.gif'; break;
case 'PDF': $icon = 'pdf.gif'; break;
case 'GZ': $icon = 'zip.gif'; break;
case 'TAR': $icon = 'zip.gif'; break;
case 'RAR': $icon = 'zip.gif'; break;
case 'TXT': $icon = 'txt.gif'; break;
case 'ZIP': $icon = 'zip.gif'; break;
default: $icon = 'file.jpg';
}

$filetype = "<img src="$icon.">";[/code]
Copy linkTweet thisAlerts:
@shane_carrAug 11.2006 — I didn't know of that function pcthug, nice.
Copy linkTweet thisAlerts:
@balloonbuffoonAug 11.2006 — How about this:
[code=php]$file = pathinfo($filef);
$type = strtoupper($file["extension"]);

$filetypes = array(
"BAT" => "bat",
"EXE" => "exe",
"GIF" => "img",
"JPG" => "img",
"PNG" => "img",
"TIF" => "img",
"TIFF" => "img",
"DOC" => "doc",
"XLS" => "xls",
"PPT" => "ppt",
"PDF" => "pdf",
"GZ" => "zip",
"TAR" => "zip",
"RAR" => "zip",
"TXT" => "txt",
"ZIP" => "zip"
);

$icon = (isset($filetypes[$type])) ? $filetypes[$type] : "file";
$filetype = "<img src='$icon.gif'>";[/code]
Or better yet, since a lot of the icons are the same name as the file extension:[code=php]$file = pathinfo("filename.ext");
$type = strtoupper($file["extension"]);

$filetypes = array(
"GIF" => "img",
"JPG" => "img",
"PNG" => "img",
"TIF" => "img",
"TIFF" => "img",
"GZ" => "zip",
"TAR" => "zip",
"RAR" => "zip"
);

$icon = file_exists($type.".gif") ? $type : (isset($filetypes[$type]) ? $filetypes[$type] : "file");
$filetype = "<img src='$icon.gif'>";
[/code]
That way, you only have to specify types which don't have an icon named after them.

--Steve
×

Success!

Help @Peuplarchie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...