index.php file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • budaim
    New Member
    • Oct 2008
    • 7

    index.php file

    Hi,

    I am new to php. I was trying to write a code for a index.php file that would only list the htm or html files in the given directory. I don't want to to list the sub directorys or any other files or folders that might be in the same folder.

    Can anyone help me please
    Thank you
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    So how far did you get?

    Post the code using [code] tags.

    Comment

    • budaim
      New Member
      • Oct 2008
      • 7

      #3
      It was 2 codes i got off php.net that I was trying to figure out how to combind them.

      This one:

      Code:
      <?php
      $current=getcwd();
      $directory=basename($current);
      $direct=str_replace('-',' ',$directory);
      $directoryuppercasewords=ucwords($direct);
      
      /*this first section above will  get your current folder or directory and since I have a "-" between the words of my file I have to strip them to look good if I want to show the current folder or directory in the title of the first link
      */
      $dir = '.';
      $dh  = opendir($dir);
      while (false !== ($filename = readdir($dh))) {
          $files[] =$filename;
      }
      $t=array_slice($files,2);
      $f=array_search('gif',$t);
      /*this code above with the variable $t gets rid of the '.' and the '..' parent and ancestor files. And the variable $f finds the index file and with the code below 'unset' let you use $f as the key to get rid of that file.
      */
      unset($t[$f]);
        print('<ul align="left">');
          print('<li><a href="." title="'.$directoryuppercasewords.'">Back</a></br></li>');
        foreach($t as $key => $value){
          $phpkill=str_replace('.php', '', $value);
          $htmlkill=str_replace('.html', '', $phpkill);
          $dashkill=str_replace('-',' ',$htmlkill);
          $uppercasewords=ucwords($dashkill);
          
          print('<li><a href="' . $dir . '/' . $value . '" title="'.$uppercasewords.'">' . $uppercasewords . "</a></br></li>");
        }
        print('</ul>');
           closedir($dh);
      ?>
      and this one:

      <
      Code:
      ?php
      function scanDirectories($rootDir, $allowext, $allData=array()) {
          $dirContent = scandir($rootDir);
          foreach($dirContent as $key => $content) {
              $path = $rootDir.'/'.$content;
              $ext = substr($content, strrpos($content, '.') + 1);
              
              if(in_array($ext, $allowext)) {
                  if(is_file($path) && is_readable($path)) {
                      $allData[] = $path;
                  }elseif(is_dir($path) && is_readable($path)) {
                      // recursive callback to open new directory
                      $allData = scanDirectories($path, $allData);
                  }
              }
          }
          return $allData;
      }
      
      $rootDir = ".";
      $allowext = array("zip","rar","html","htm");
      $files_array = scanDirectories($rootDir,$allowext);
      print_r(array_slice($files_array));
      ?>

      Comment

      • trochia
        New Member
        • Oct 2008
        • 19

        #4
        I'm fairly new also and I have used this once....but haven't done any html formatting for output with it. But the combination of these (2) looks like a lot less code than what you going..??

        [PHP]<?php
        $files = scandir(".", 1);
        var_dump($files );
        ?>
        [/PHP]

        From here: http://hudzilla.org/phpwiki/index.php?title =Working_with_d irectories

        var_dump

        and I see it here:
        http://us.php.net/manual/en/function.scandi r.php

        http://hudzilla.org/phpwiki/index.php?title =Advanced_file_ upload_handling

        And somewhere in his wiki, under "file upload types" is code for checking for allowed...etc.. . then run a print output into a table?

        Hope this helps...I don't have time to play with yours, as I am still learning also, and unable to get this sessions to DB table to work for me ..



        Jim

        Comment

        • budaim
          New Member
          • Oct 2008
          • 7

          #5
          Thank you very much. I am still stumped, I will read on untill I hopefully figure it out. If anyone can help me that would be awesome.

          Thanks again

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Hi.

            In your case, the glob function wold probably be the best way:
            [code=php]
            <?php
            $html = glob('*.html');
            print_r($files) ;
            ?>
            [/code]
            Which might print something like:
            Code:
            Array
            (
                [0] => index.html
                [1] => test.html
            )
            And if you need to search for more than one type, the simplest way to do so would probably be to call glob for each of them and merge the result arrays using array_merge.

            Comment

            • budaim
              New Member
              • Oct 2008
              • 7

              #7
              where would i add that into the code?

              Comment

              • budaim
                New Member
                • Oct 2008
                • 7

                #8
                This is the code that I got to work for me

                Code:
                <?php 
                function scanDirectories($rootDir, $allowext, $allData=array()) { 
                    $dirContent = scandir($rootDir); 
                    foreach($dirContent as $key => $content) { 
                        $path = $rootDir.'/'.$content; 
                        $ext = substr($content, strrpos($content, '.') + 1); 
                  
                        if(in_array($ext, $allowext)) { 
                            if(is_file($path) && is_readable($path)) { 
                                $allData[] = $path; 
                            }elseif(is_dir($path) && is_readable($path)) { 
                                // recursive callback to open new directory 
                                $allData = scanDirectories($path, $allData); 
                            } 
                        } 
                    } 
                    return $allData; 
                } 
                  
                $rootDir = "."; 
                $allowext = array("htm","html"); 
                $files_array = scanDirectories($rootDir,$allowext); 
                $t=array_slice($files_array,2);
                $f=array_search('gif',$t); 
                /*this code above with the variable $t gets rid of the '.' and the '..' parent and ancestor files. And the variable $f finds the index file and with the code below 'unset' let you use $f as the key to get rid of that file. 
                */ 
                unset($t[$f]); 
                  print('<ul align="left">'); 
                    print('<li><a href="." title="'.$directoryuppercasewords.'">Back</a></br></li>'); 
                  foreach($t as $key => $value){ 
                    $phpkill=str_replace('.php', '', $value); 
                    $htmlkill=str_replace('.html', '', $phpkill); 
                	 $htmkill=str_replace('.htm', '', $htmlkill); 
                	 $crapkill=str_replace('./', '', $htmkill); 
                    $dashkill=str_replace('-',' ',$crapkill); 
                    $uppercasewords=ucwords($dashkill); 
                  
                    print('<li><a href="' . $rootDir . '/' . $value . '" title="'.$uppercasewords.'">' . $uppercasewords . "</a></br></li>"); 
                  } 
                  print('</ul>'); 
                
                   
                ?>

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by budaim
                  where would i add that into the code?
                  This is the code...
                  Put that into a .php file, put the file into a directory and execute it and it will show you a list of all .html files in that directory.

                  Comment

                  • budaim
                    New Member
                    • Oct 2008
                    • 7

                    #10
                    This is the final code that I am using. I replaced array_slice with array_values so It would not kill my 0 value. and I removed array_search because it was also removing my 0 value

                    Code:
                    <?php 
                    function scanDirectories($rootDir, $allowext, $allData=array()) { 
                        $dirContent = scandir($rootDir); 
                        foreach($dirContent as $key => $content) { 
                            $path = $rootDir.'/'.$content; 
                            $ext = substr($content, strrpos($content, '.') + 1); 
                      
                            if(in_array($ext, $allowext)) { 
                                if(is_file($path) && is_readable($path)) { 
                                    $allData[] = $path; 
                                }elseif(is_dir($path) && is_readable($path)) { 
                                    // recursive callback to open new directory 
                                    $allData = scanDirectories($path, $allData); 
                                } 
                            } 
                        } 
                        return $allData; 
                    } 
                      
                    $rootDir = "."; 
                    $allowext = array("htm","html"); 
                    $files_array = scanDirectories($rootDir,$allowext); 
                    $t=array_values($files_array);
                    /*this code above with the variable $t gets rid of the '.' and the '..' parent and ancestor files. And the variable $f finds the index file and with the code below 'unset' let you use $f as the key to get rid of that file. 
                    */ 
                     
                    
                      foreach($t as $key => $value){ 
                        $phpkill=str_replace('.php', '', $value); 
                        $htmlkill=str_replace('.html', '', $phpkill); 
                    	 $htmkill=str_replace('.htm', '', $htmlkill); 
                    	 $crapkill=str_replace('./', '', $htmkill); 
                        $dashkill=str_replace('-',' ',$crapkill); 
                        $uppercasewords=ucwords($dashkill); 
                      
                        print('<li><a href="' . $rootDir . '/' . $value . '" title="'.$uppercasewords.'">' . $uppercasewords . "</a></br></li>"); 
                      } 
                      print('</ul>'); 
                    
                       
                    ?>

                    Comment

                    • budaim
                      New Member
                      • Oct 2008
                      • 7

                      #11
                      Originally posted by Atli
                      This is the code...
                      Put that into a .php file, put the file into a directory and execute it and it will show you a list of all .html files in that directory.

                      When I did that, nothing showed up. Maybe its my version of php? Anywho I am using that code i posted above. THANK YOU EVERYONE FOR ALL YOUR HELP.

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        I'm glad you found a solution!

                        By the way, I mixed up the variables in my example. It should have been:
                        [code=php]
                        <?php
                        $files = glob('*.html');
                        print_r($files) ;
                        ?>
                        [/code]
                        That should work :)

                        Comment

                        Working...