Results 1 to 13 of 13

Thread: show files

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8

    show files

    i want to know how can i show all the files in a directory? thanks

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I posted code similiar to this not too long ago:

    Code:
    <?php
    if ($handle = opendir('/home/your_site/folder')) {
        while (false !== ($file = readdir($handle))) { 
            echo "$file<br>\n";
        }
    
        closedir($handle); 
    }
    ?>
    Hope this helps.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8
    how do i make only show files?

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Opps. Sorry about that. Just use the is_file() function to check to see if it's a file:

    Code:
    <?php
        if ($handle = opendir('/home/your_site/folder')) {
            while (false !== ($file = readdir($handle))) { 
                if (is_file($file)) {
                    echo "$file<br>\n";
                }
            }
    
            closedir($handle); 
        }
    ?>
    These functions and code snippets can be found in the manual at www.php.net It's an invaluable resource.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8
    thanks i have this code

    <?php
    echo '<table>';

    if ($handle = opendir('C:/Site/dks/')) {
    while (false !== ($file = readdir($handle))) {
    if (is_file($file)) {
    echo '<tr><td>' . $file . '</td><td>FILESIZE</td></tr>';
    }
    }

    closedir($handle);
    }

    echo '</table>';
    ?>

    how do i filesize?

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by php_n00b
    how do i filesize?
    Did you not follow the link I gave you?

    Here is a link o File System Functions and Directory Functions.

    Check out the function called filesize().
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This is how you'd do it:

    Code:
    <?php
        echo '<table>';
    
        if ($handle = opendir('C:/Site/dks/')) {
            while (false !== ($file = readdir($handle))) { 
                if (is_file($file)) {
                    echo '<tr><td>' . $file . '</td><td>' . formatsize(filesize($file)) . '</td></tr>';
                }
            }
    
            closedir($handle); 
        }
        
        echo '</table>';
    
        function formatsize($fs) {
          if($fs < 1024) {
               return $fs . " B";
          } else if($fs < pow(1024, 2)) {
              return ceil($fs / 1024) . " KB";
    
          } else {
               return ceil($fs / pow(1024, 2)) . " MB";
          }
        }
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8
    why not in alpha order???

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You're probably going to have to load the file names into an array, then use the sort() function, then loop through and echo them out.

    I'll post code in a minute...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This should work:

    Code:
    <?php
    
        //get the files:
        if ($handle = opendir('C:/Site/dks/')) {
            $i = 0;
            while (false !== ($file = readdir($handle))) { 
                if (is_file($file)) {
                    //add file to array:
                    $files[$i][0] = $file;
                    $files[$i][1] = formatsize(filesize($file));
    
                    $i++;
                }
            }
    
            closedir($handle); 
        }
        
        //sort the array:
        sort($files);
    
        //output the files:
        echo '<table>';
        for ($i = 0; $i < count($files); $i++) {
            echo '<tr><td>' . $files[$i][0] . '</td><td>' . $files[$i][1] . '</td></tr>';
        }
        echo '</table>';
    
    
        //function to format file size:
        function formatsize($fs) {
          if($fs < 1024) {
               return $fs . " B";
          } else if($fs < pow(1024, 2)) {
              return ceil($fs / 1024) . " KB";
    
          } else {
               return ceil($fs / pow(1024, 2)) . " MB";
          }
        }
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    can you put the ones that were created today in pink twotwos



    should have made him look for how to do it....

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    can you put the ones that were created today in pink twotwos
    For that you'll have to load the fd_si library and use the _SetT() function.

    I'll post code in a minute.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    Another way:
    Code:
    		$dYre = 'your folder, like /uploads or /myDir';
    		$dh = opendir($dYre);
    		
    		while (gettype($file = readdir($dh)) != 'boolean')
    		{
    			if (is_dir("$dYre/$file"))
    				print '<font color=777777><b>[Directory]</b> ';
    
    			if (is_file("$dYre/$file"))
    				print '<font color=FF0000><b>[File]</b> ';
    			print $file.'</font><br>';
    		}
    		closedir($dh);
    asdf

Posting Permissions

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



Click Here to Expand Forum to Full Width