Results 1 to 3 of 3

Thread: I need help modifying a script

  1. #1
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default I need help modifying a script

    This is the script:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       <title>Display Directory Contents</title>
       <style>
         *{
             padding:0;
             margin:0;
         }
         html,body {
             color:#333;
             font-family: "Lucida Console", Courier, monospace;
             font-size:14px;
             text-shadow:1px 1px 1px #cacaca;
             -webkit-text-shadow:1px 1px 1px #cacaca;
             -moz-text-shadow:1px 1px 1px #cacaca;
         }
         a{
             padding: 2px 0 0 24px;
             color:#FE4902;
             text-decoration:none;
         }
         a:hover{
             color:#000;
         }
         #container{
             margin:0 auto;
             width:700px;
             margin-top:20px;
             padding-top:10px;
             border:1px solid #EEE;
             border-radius:10px;
             -moz-border-radius:10px;
         }
         .head{
             background-color:#FE4902;
             color:#FFF;
             font-weight:bold;
             padding:7px 0 5px 10px;
             font-size:14px;
             letter-spacing:1px;
             font-family: Verdana, Arial, Helvetica, sans-serif;
         }
         .head:hover{background-color:#FE4902;}
         .head span{font-size:9px; letter-spacing:0;}
         td{
             background-color:#F3F3F3;
             padding:6px;
         }
         td:hover{background-color:#EFEFEF;}
         h1{
             font-size:18px;
             font-weight:bold;
             padding:0 0 10px 10px;
         }
    
         /*icons for file types (add more to suit your needs - icons by famfamfam.)*/
    
         /*images*/
         a[href$=".jpg"] {background: url(image.gif) no-repeat left 50%;}
         a[href$=".gif"] {background: url(image.gif) no-repeat left 50%;}
         a[href$=".png"] {background: url(image.gif) no-repeat left 0%;}
    
         /*pdfs*/
         a[href$=".pdf"] {background: url(pdf.gif) no-repeat left 50%;}
    
         /*psds*/
         a[href$=".psd"] {background: url(psd.gif) no-repeat left 50%;}
    
         /*docs*/
         a[href$=".doc"] {background: url(doc.gif) no-repeat left 50%;}
         a[href$=".txt"] {background: url(doc.gif) no-repeat left 50%;}
    
         /*videos*/
         a[href$=".avi"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".m4a"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".mov"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".mp4"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".wmv"] {background: url(video.gif) no-repeat left 50%;}
    
         /*audio*/
         a[href$=".mp3"] {background: url(audio.gif) no-repeat left 50%;}
         a[href$=".wma"] {background: url(audio.gif) no-repeat left 50%;}
         a[href$=".aac"] {background: url(audio.gif) no-repeat left 50%;}
    
         /*web pages*/
         a[href$=".html"] {background: url(html.gif) no-repeat left 50%;}
         a[href$=".php"] {background: url(html.gif) no-repeat left 50%;}
    
       </style>
    
    </head>
    <body>
    
       <div id="container">
           <?php
             // opens this directory
             $myDirectory = opendir(".");
    
             // gets each entry
             while($entryName = readdir($myDirectory)) {
               $dirArray[] = $entryName;
             }
    
             // finds extention of file
             function findexts ($filename)
             {
               $filename = strtolower($filename) ;
               $exts = split("[/\\.]", $filename) ;
               $n = count($exts)-1;
               $exts = $exts[$n];
               return $exts;
             }
    
             // closes directory
             closedir($myDirectory);
    
             //  counts elements in array
             $indexCount   = count($dirArray);
    
             // sorts files
             sort($dirArray);
    
             // print 'em
             print("<h1>Directory Contents</h1>");
             print("<table width='100%' cellspacing='10'>
                     <tr>
                       <td class='head'>Filename</td>
                       <td class='head'>Type</td>
                       <td class='head'>Size <span>(bytes)</span></td></tr>\n");
    
             // loops through the array of files and print them all
             for($index=0; $index < $indexCount; $index++) {
                   if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
                   print("<tr><td><a href='$dirArray[$index]'>$dirArray[$index]</a></td>");
                   print("<td>");
                   print(findexts($dirArray[$index]));
                   print("</td>");
                   print("<td>");
                   print(filesize($dirArray[$index]));
                   print("</td>");
                   print("</tr>\n");
               }
             }
             print("</table>\n");
           ?>
       </div>
    
    </body>
    </html>
    I found it here at:
    http://css-tricks.com/snippets/php/d...tory-contents/

    I posted and got no answer .I would like the directorys that are put out to also display the image for a directory.Besides adding the part of the image ,what else do I need to do to get that to show next to the directorys in the output.

    Bud
    Last edited by ajfmrf; 02-17-2012 at 02:38 AM.
    Thanks,

    Bud

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The icons for all of the other extensions are arrived at through css style, ex:

    Code:
    a[href$=".html"] {background: url(html.gif) no-repeat left 50%;}
    That's just plain css. It says that all a tags with an href attribute ending in .html are to use the html.gif background image.

    There are similar style rules for all of the extensions that the person who coded this had in mind when they wrote it.

    Perhaps the simplest approach would be to assume that, if the item in the listing has none of these extensions, it's a directory. So then all you would have to do is before all of these more specific rules, have one like so:

    Code:
         td:hover{background-color:#EFEFEF;}
         h1{
             font-size:18px;
             font-weight:bold;
             padding:0 0 10px 10px;
         }
    
         /*icons for file types (add more to suit your needs - icons by famfamfam.)*/
         
         /* All others assumed to be folders */
         a[href] {background: url(folder.gif) no-repeat left 50%;}
    
         /*images*/
         a[href$=".jpg"] {background: url(image.gif) no-repeat left 50%;}
         a[href$=".gif"] {background: url(image.gif) no-repeat left 50%;}
         a[href$=".png"] {background: url(image.gif) no-repeat left 0%;}
    
         /*pdfs*/
         a[href$=".pdf"] {background: url(pd
    Here's a link to a suitable image:

    http://upload.wikimedia.org/wikipedi...ini-folder.gif

    I used it, renaming it to folder.gif and it worked just fine, except that unknown file types also got it (.zip and .htm to name two). But that can be remedied by adding css rules and images for those. The .htm extension can be added here:

    Code:
         /*web pages*/
         a[href$=".html"] {background: url(html.gif) no-repeat left 50%;}
         a[href$=".htm"] {background: url(html.gif) no-repeat left 50%;}
         a[href$=".php"] {background: url(html.gif) no-repeat left 50%;}
    
       </style>
    
    </head>
    <body>
    
       <div id="con . . .
    For .zip, you would need an icon - say zip.gif and a rule like:

    Code:
         a[href$=".zip"] {background: url(zip.gif) no-repeat left 50%;}
    Here's a nice one:

    http://commerce.nic.in/images/zip.gif

    But I prefer this modified version. It uses the PHP function is_dir() to find which items are folders and assigns the folder class and type to them (additions/changes highlighted):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       <title>Display Directory Contents</title>
       <style>
         *{
             padding:0;
             margin:0;
         }
         html,body {
             color:#333;
             font-family: "Lucida Console", Courier, monospace;
             font-size:14px;
             text-shadow:1px 1px 1px #cacaca;
             -webkit-text-shadow:1px 1px 1px #cacaca;
             -moz-text-shadow:1px 1px 1px #cacaca;
         }
         a{
             padding: 2px 0 0 24px;
             color:#FE4902;
             text-decoration:none;
         }
         a:hover{
             color:#000;
         }
         #container{
             margin:0 auto;
             width:700px;
             margin-top:20px;
             padding-top:10px;
             border:1px solid #EEE;
             border-radius:10px;
             -moz-border-radius:10px;
         }
         .head{
             background-color:#FE4902;
             color:#FFF;
             font-weight:bold;
             padding:7px 0 5px 10px;
             font-size:14px;
             letter-spacing:1px;
             font-family: Verdana, Arial, Helvetica, sans-serif;
         }
         .head:hover{background-color:#FE4902;}
         .head span{font-size:9px; letter-spacing:0;}
         td{
             background-color:#F3F3F3;
             padding:6px;
         }
         td:hover{background-color:#EFEFEF;}
         h1{
             font-size:18px;
             font-weight:bold;
             padding:0 0 10px 10px;
         }
    
         /*icons for file types (add more to suit your needs - icons by famfamfam.)*/
         
         /* those with folder class assumed to be folders */
         a.folder {background: url(folder.gif) no-repeat left 50%;}
    
         /*images*/
         a[href$=".jpg"] {background: url(image.gif) no-repeat left 50%;}
         a[href$=".gif"] {background: url(image.gif) no-repeat left 50%;}
         a[href$=".png"] {background: url(image.gif) no-repeat left 0%;}
    
         /*pdfs*/
         a[href$=".pdf"] {background: url(pdf.gif) no-repeat left 50%;}
    
         /*psds*/
         a[href$=".psd"] {background: url(psd.gif) no-repeat left 50%;}
    
         /*docs*/
         a[href$=".doc"] {background: url(doc.gif) no-repeat left 50%;}
         a[href$=".txt"] {background: url(doc.gif) no-repeat left 50%;}
    
         /*videos*/
         a[href$=".avi"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".m4a"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".mov"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".mp4"] {background: url(video.gif) no-repeat left 50%;}
         a[href$=".wmv"] {background: url(video.gif) no-repeat left 50%;}
    
         /*audio*/
         a[href$=".mp3"] {background: url(audio.gif) no-repeat left 50%;}
         a[href$=".wma"] {background: url(audio.gif) no-repeat left 50%;}
         a[href$=".aac"] {background: url(audio.gif) no-repeat left 50%;}
    
         /*web pages*/
         a[href$=".html"] {background: url(html.gif) no-repeat left 50%;}
         a[href$=".htm"] {background: url(html.gif) no-repeat left 50%;}
         a[href$=".php"] {background: url(html.gif) no-repeat left 50%;}
         
         /* compressed */
         a[href$=".zip"] {background: url(zip.gif) no-repeat left 50%;}
    
       </style>
    
    </head>
    <body>
    
       <div id="container">
           <?php
             // opens this directory
             $myDirectory = opendir(".");
    
             // gets each entry
             while($entryName = readdir($myDirectory)) {
               $dirArray[] = $entryName;
             }
    
             // finds extention of file
             function findexts ($filename)
             {
               if(is_dir($filename)){return 'folder';}
               $filename = strtolower($filename) ;
               $exts = preg_split('/\\./', $filename) ;
               $n = count($exts)-1;
               $exts = $exts[$n];
               return $exts;
             }
    
             // closes directory
             closedir($myDirectory);
    
             //  counts elements in array
             $indexCount   = count($dirArray);
    
             // sorts files
             sort($dirArray);
    
             // print 'em
             print("<h1>Directory Contents</h1>");
             print("<table width='100%' cellspacing='10'>
                     <tr>
                       <td class='head'>Filename</td>
                       <td class='head'>Type</td>
                       <td class='head'>Size <span>(bytes)</span></td></tr>\n");
    
             // loops through the array of files and print them all
             for($index=0; $index < $indexCount; $index++) {
                   if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
                   if (findexts($dirArray[$index]) == 'folder'){
                     print("<tr><td><a class='folder' href='$dirArray[$index]'>$dirArray[$index]</a></td>");
                   } else {
                     print("<tr><td><a href='$dirArray[$index]'>$dirArray[$index]</a></td>");
                   }
                   print("<td>");
                   print(findexts($dirArray[$index]));
                   print("</td>");
                   print("<td>");
                   print(filesize($dirArray[$index]));
                   print("</td>");
                   print("</tr>\n");
               }
             }
             print("</table>\n");
           ?>
       </div>
    
    </body>
    </html>
    Still need a folder icon to satisfy this added css rule:

    Code:
         /* those with folder class assumed to be folders */
         a.folder {background: url(folder.gif) no-repeat left 50%;}
    Last edited by jscheuer1; 02-17-2012 at 01:06 AM. Reason: include alternate code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ajfmrf (02-17-2012)

  4. #3
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    Well done John-thanks again
    Thanks,

    Bud

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •