Display filenames in 1 php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Display filenames in 1 php

Post by parkin »

Hi..
I'm kinda stuck at how about going to retrieve filenames... :?
this is wht I'd like to do..

I wanna display the file/folder names..
the folders are this way:
--> documents
    --> new
          --> file01.php
    --> old
          --> file02.php
          --> file03.php
What I'd like is that the first display is
New
Old

then when i click on "New" it displays file01.php
or when I click on "Old" it displays file02.php, file03.php

I'd like to do this all in one php and place that php file in the documents folder...

Can anyone help? :mrgreen:

So far.. I'm up to here:

Code: Select all

<?php
    $base = "../document/";
    $base_array = array();
    
     if(is_dir($base)) // if it is a dir
    {
         $diropen = opendir($base); // open dir
         
         while (false !== ($dir = readdir($diropen)))  // stop when $dir=false
         {
              // get rid of the folders that need not be displayed
              // if base.subdir name is a dir
              if (is_dir($base.$dir) && $dir !== '.' && $dir !== '..' && $dir !== 'drop box'  && $dir !== '.DS_Store') 
               {
                   $subs = $dir;
                   $subbase = strval($base.$dir."/"); // entire filename
                   
                   //print "subbase: $subbase<br>";
                   print "<a href="$dir/">$dir</a><br>";
                   
                   
              } 
               
              if(is_file($base.$dir) && $dir !== '.' && $dir !== '..' && $dir !== 'drop box'  && $dir !== '.DS_Store')
              {
                   $subs = $dir;
                   $subbase = strval($base.$dir); // entire filename
                   
         
                        if(!strstr($dir, ".php")) // take out the .php files
                        {                    
         
                             
                        }
                   
              }
              
          }
         closedir($diropen); // close dir
         
    } 
     
    else  // if it is not a dir
    { 
          print "no dir";
         next;
    }
   ?>
      
need help!!!
thanks :wink:
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

I'm just gonna type up my own little version here, but be sure to check through it for parse errors first if you choose to use it

Code: Select all

<?
/* KUDOS johnpipi at hotmail dot com */
function getDirFiles($dirPath){
	if ($handle = opendir($dirPath)) {
		while (false !== ($file = readdir($handle))){
			if ($file != "." && $file != ".."){
				$filesArr[] = trim($file);
			}
		}
		closedir($handle);
	}

	return $filesArr; 
}

$BASE_PATH = "/tmp";

$path = (!isset($_GET['path']))?$BASE_PATH:$_GET['path'];

$array = getDirFiles($path);

foreach ($array as $value){
	if (is_dir($value)){
		echo "<a href={$PHP_SELF}?path={$path}{$value}>{$value}</a><br />\r\n";
	} else {
 		echo "{$value}<br />\r\n";
	}
}
?>
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Post by parkin »

Hey.. Thanks qartis! :D

It helped alot.. I'm finally done! :mrgreen:
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

thanks man, that's cool,
can we count the contents?

Code: Select all

<?php
function foldercount($folder)
{
$handle=opendir($folder); // istedi&#287;in folder.

echo "<h1>Files:</h1><br>";
$i=0;
while (false !== ($file = readdir($handle))) { 
    if ($file != "." && $file != "..") { 
        echo "$file<br>"; 
		$i++;
    } 
}
echo $i; // say&#305;y&#305; yazd&#305;r&#305;r.

closedir($handle); 
}

foldercount("/web/");
?>
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Code: Select all

<? 
/* KUDOS johnpipi at hotmail dot com */ 
function getDirFiles($dirPath){
   if ($handle = opendir($dirPath)) { 
      while (false !== ($file = readdir($handle))){ 
         if ($file != "." && $file != ".."){ 
            $filesArr[] = trim($file); 
         } 
      } 
      closedir($handle); 
   } 
 
   return $filesArr; 
} 

function wotsit ($var) {
    is_folder($var)?return 0:return 1;
}

$BASE_PATH = "/tmp"; 
 
$path = (!isset($_GET['path']))?$BASE_PATH:$_GET['path']; 
 
$array = getDirFiles($path); 

usort ($array, "wotsit");

echo "<h1>".count($array)." elements</h1><br />\r\n";

foreach ($array as $value){ 
   if (is_dir($value)){ 
      echo "<a href={$PHP_SELF}?path={$path}{$value}>{$value}</a><br />\r\n"; 
   } else { 
       echo "{$value}<br />\r\n"; 
   } 
} 
?>

.. not sure if it'll work perfectissimo, but it's just a proof-of-concept.

If you want me to add folder/file count or stuff like that, just ask :)
Post Reply