PHP/File Directory/is dir

Материал из Web эксперт
Перейти к: навигация, поиск

Creating a Function That Performs Multiple File Tests

   <source lang="html4strict">

<?php function outputFileTestInfo( $file ) {

 if ( ! file_exists( $file ) ) {
   print "$file does not exist
"; return; } print "$file is ".( is_file( $file )?"":"not ")."a file
"; print "$file is ".( is_dir( $file )?"":"not ")."a directory
"; print "$file is ".( is_readable( $file )?"":"not ")."readable
"; print "$file is ".( is_writable( $file )?"":"not ")."writable
"; print "$file is ".( is_executable( $file )?"":"not")."executable
"; print "$file is ".( filesize($file))." bytes
"; print "$file was accessed on " . date( "D d M Y g:i A", fileatime( $file ) )."
"; print "$file was modified on " . date( "D d M Y g:i A", filemtime( $file ) )."
"; print "$file was changed on " . date( "D d M Y g:i A", filectime( $file ) )."
";

} ?> <html> <head> <title>Multiple File Tests</title> </head> <body>

<?php outputFileTestInfo( "test.txt" ); ?>

</body> </html>

 </source>
   
  


Is a directory

   <source lang="html4strict">

<? if ( is_dir( "/tmp" ) ) {

 print "/tmp is a directory";

} ?>

 </source>
   
  


is_dir() function verifies that the file is a directory

   <source lang="html4strict">

bool is_dir (string filename) <?

   $isdir = is_dir("index.html"); // returns false
   
   $isdir = is_dir("book"); // returns true

?>

 </source>
   
  


Working with Directories

   <source lang="html4strict">

resource opendir ( string path ) bool is_dir ( string path ) string readdir ( resource dir_handle ) void closedir ( resource dir_handle ) array scandir ( string directory [, int sorting_order [, resource context]] ) <?php function numfilesindir ($thedir){

   if (is_dir ($thedir)){ 
       $scanarray = scandir ($thedir); 
       for ($i = 0; $i < count ($scanarray); $i++){ 
           if ($scanarray[$i] != "." && $scanarray[$i] != ".."){ 
               if (is_file ($thedir . "/" . $scanarray[$i])){ 
                   echo $scanarray[$i] . "
"; } } } } else { echo "Sorry, this directory does not exist."; }

} echo numfilesindir ("sample1"); ?>

<?

   $handle = opendir("/path/to/directory")
   if ($handle) {
           while (false !=  = ($file = readdir($handle))) {
                   print "$file
\n"; } closedir($handle); }

?>

 </source>