I'm creating a test file to get all files located in an intranet folder and simply put an icon image next to the file listed according to its extension. For some reason, the code is not pulling things correctly.
Here's a screen shot of the windows explorer view of the files in the directory.
And here is my code:
<?php
$dir="file:///.....to_my_directory"; // Directory where files are stored
if ($dir_list = opendir($dir)) {
while(($filename = readdir($dir_list)) != false) {
if (is_dir($filename)) {
$img = 'folder_img.jpg';
} else {
$filestuff = pathinfo($filename);
$file_ext = isset($filestuff['extension']) ? $filestuff['extension'] : null;
switch($file_ext) {
case 'pdf':
$img = 'pdf_img.jpg';
break;
case 'doc' || 'docx' || 'docm' || 'dotm' || 'dotx':
$img = 'word_img.jpg';
break;
case 'xls' || 'xlsm' || 'xltx' || 'xlam' || 'xlsx' || 'xlm' || 'xlt' || 'xlsb':
$img = 'excel_img.jpg';
break;
case 'txt':
$img = 'txt_file_img.jpg';
break;
case NULL:
$img = 'blank_file_img.jpg';
break;
}
}
?>
<p><span><img src='<?php echo $img; ?>' height='23px;'/><a href="<?php echo $filename; ?>"><?php echo $filename;?></a></span></p>
<?php
}
closedir($dir_list);
}
?>
AND this is how it looks when I run it:
The problem is that certain extensions not listed are pulling the 'Microsoft WORD' icon.
The 'Access Database' file with extension '.mdb' is pulling a Word icon, the extension '.mdb_hcu' is also pulling a WORD icon.
How do I make it the BLANK file option (see my switch case where answer is NULL)
Also, how do I organize so that the folders are listed first and everything is in alphabetical order?
Is there a plugin that allows me to achieve this easier?
Problem is that you can't list case values like this:
case 'doc' || 'docx' || 'docm' || 'dotm' || 'dotx':
That will get evaluated as one statement which will evaluate to true. Then the extension will compare to that result and unless the extension is NULL
, the case will be true.
You need to list out each case option like so:
case 'doc': //if
case 'docx': //any
case 'docm': //of
case 'dotm': //these
case 'dotx': //are true
//this will run
//set image
break; //up to this
If any of the cases are true (equal to the extension), the code inside the case will run until the break
is hit.