By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

index.php file

P: 7
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
Oct 21 '08 #1
Share this Question
Share on Google+
11 Replies


Markus
Expert 5K+
P: 6,050
So how far did you get?

Post the code using [code] tags.
Oct 21 '08 #2

P: 7
It was 2 codes i got off php.net that I was trying to figure out how to combind them.

This one:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $current=getcwd();
  3. $directory=basename($current);
  4. $direct=str_replace('-',' ',$directory);
  5. $directoryuppercasewords=ucwords($direct);
  6.  
  7. /*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
  8. */
  9. $dir = '.';
  10. $dh  = opendir($dir);
  11. while (false !== ($filename = readdir($dh))) {
  12.     $files[] =$filename;
  13. }
  14. $t=array_slice($files,2);
  15. $f=array_search('gif',$t);
  16. /*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.
  17. */
  18. unset($t[$f]);
  19.   print('<ul align="left">');
  20.     print('<li><a href="." title="'.$directoryuppercasewords.'">Back</a></br></li>');
  21.   foreach($t as $key => $value){
  22.     $phpkill=str_replace('.php', '', $value);
  23.     $htmlkill=str_replace('.html', '', $phpkill);
  24.     $dashkill=str_replace('-',' ',$htmlkill);
  25.     $uppercasewords=ucwords($dashkill);
  26.  
  27.     print('<li><a href="' . $dir . '/' . $value . '" title="'.$uppercasewords.'">' . $uppercasewords . "</a></br></li>");
  28.   }
  29.   print('</ul>');
  30.      closedir($dh);
  31. ?> 
and this one:

<
Expand|Select|Wrap|Line Numbers
  1. ?php
  2. function scanDirectories($rootDir, $allowext, $allData=array()) {
  3.     $dirContent = scandir($rootDir);
  4.     foreach($dirContent as $key => $content) {
  5.         $path = $rootDir.'/'.$content;
  6.         $ext = substr($content, strrpos($content, '.') + 1);
  7.  
  8.         if(in_array($ext, $allowext)) {
  9.             if(is_file($path) && is_readable($path)) {
  10.                 $allData[] = $path;
  11.             }elseif(is_dir($path) && is_readable($path)) {
  12.                 // recursive callback to open new directory
  13.                 $allData = scanDirectories($path, $allData);
  14.             }
  15.         }
  16.     }
  17.     return $allData;
  18. }
  19.  
  20. $rootDir = ".";
  21. $allowext = array("zip","rar","html","htm");
  22. $files_array = scanDirectories($rootDir,$allowext);
  23. print_r(array_slice($files_array));
  24. ?>
Oct 21 '08 #3

P: 18
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_directories

var_dump

and I see it here:
http://us.php.net/manual/en/function.scandir.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 ..

http://hudzilla.org/phpwiki/index.ph...s_for_sessions

Jim
Oct 22 '08 #4

P: 7
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
Oct 22 '08 #5

Atli
Expert 5K+
P: 5,058
Hi.

In your case, the glob function wold probably be the best way:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $html = glob('*.html');
  3. print_r($files);
  4. ?>
  5.  
Which might print something like:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => index.html
  4.     [1] => test.html
  5. )
  6.  
  7.  
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.
Oct 22 '08 #6

P: 7
where would i add that into the code?
Oct 23 '08 #7

P: 7
This is the code that I got to work for me

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. function scanDirectories($rootDir, $allowext, $allData=array()) { 
  3.     $dirContent = scandir($rootDir); 
  4.     foreach($dirContent as $key => $content) { 
  5.         $path = $rootDir.'/'.$content; 
  6.         $ext = substr($content, strrpos($content, '.') + 1); 
  7.  
  8.         if(in_array($ext, $allowext)) { 
  9.             if(is_file($path) && is_readable($path)) { 
  10.                 $allData[] = $path; 
  11.             }elseif(is_dir($path) && is_readable($path)) { 
  12.                 // recursive callback to open new directory 
  13.                 $allData = scanDirectories($path, $allData); 
  14.             } 
  15.         } 
  16.     } 
  17.     return $allData; 
  18.  
  19. $rootDir = "."; 
  20. $allowext = array("htm","html"); 
  21. $files_array = scanDirectories($rootDir,$allowext); 
  22. $t=array_slice($files_array,2);
  23. $f=array_search('gif',$t); 
  24. /*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. 
  25. */ 
  26. unset($t[$f]); 
  27.   print('<ul align="left">'); 
  28.     print('<li><a href="." title="'.$directoryuppercasewords.'">Back</a></br></li>'); 
  29.   foreach($t as $key => $value){ 
  30.     $phpkill=str_replace('.php', '', $value); 
  31.     $htmlkill=str_replace('.html', '', $phpkill); 
  32.      $htmkill=str_replace('.htm', '', $htmlkill); 
  33.      $crapkill=str_replace('./', '', $htmkill); 
  34.     $dashkill=str_replace('-',' ',$crapkill); 
  35.     $uppercasewords=ucwords($dashkill); 
  36.  
  37.     print('<li><a href="' . $rootDir . '/' . $value . '" title="'.$uppercasewords.'">' . $uppercasewords . "</a></br></li>"); 
  38.   } 
  39.   print('</ul>'); 
  40.  
  41.  
  42. ?> 
Oct 23 '08 #8

Atli
Expert 5K+
P: 5,058
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.
Oct 23 '08 #9

P: 7
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

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. function scanDirectories($rootDir, $allowext, $allData=array()) { 
  3.     $dirContent = scandir($rootDir); 
  4.     foreach($dirContent as $key => $content) { 
  5.         $path = $rootDir.'/'.$content; 
  6.         $ext = substr($content, strrpos($content, '.') + 1); 
  7.  
  8.         if(in_array($ext, $allowext)) { 
  9.             if(is_file($path) && is_readable($path)) { 
  10.                 $allData[] = $path; 
  11.             }elseif(is_dir($path) && is_readable($path)) { 
  12.                 // recursive callback to open new directory 
  13.                 $allData = scanDirectories($path, $allData); 
  14.             } 
  15.         } 
  16.     } 
  17.     return $allData; 
  18.  
  19. $rootDir = "."; 
  20. $allowext = array("htm","html"); 
  21. $files_array = scanDirectories($rootDir,$allowext); 
  22. $t=array_values($files_array);
  23. /*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. 
  24. */ 
  25.  
  26.  
  27.   foreach($t as $key => $value){ 
  28.     $phpkill=str_replace('.php', '', $value); 
  29.     $htmlkill=str_replace('.html', '', $phpkill); 
  30.      $htmkill=str_replace('.htm', '', $htmlkill); 
  31.      $crapkill=str_replace('./', '', $htmkill); 
  32.     $dashkill=str_replace('-',' ',$crapkill); 
  33.     $uppercasewords=ucwords($dashkill); 
  34.  
  35.     print('<li><a href="' . $rootDir . '/' . $value . '" title="'.$uppercasewords.'">' . $uppercasewords . "</a></br></li>"); 
  36.   } 
  37.   print('</ul>'); 
  38.  
  39.  
  40. ?> 
Oct 23 '08 #10

P: 7
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.
Oct 23 '08 #11

Atli
Expert 5K+
P: 5,058
I'm glad you found a solution!

By the way, I mixed up the variables in my example. It should have been:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $files = glob('*.html');
  3. print_r($files);
  4. ?>
  5.  
That should work :)
Oct 23 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.