PHP/File Directory/is dir — различия между версиями

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

Текущая версия на 07:04, 26 мая 2010

Creating a Function That Performs Multiple File Tests

 
<?php
function outputFileTestInfo( $file ) {
  if ( ! file_exists( $file ) ) {
    print "$file does not exist<br/>";
    return;
  }
  print "$file is ".(    is_file( $file )?"":"not ")."a file<br/>";
  print "$file is ".(    is_dir( $file )?"":"not ")."a directory<br/>";
  print "$file is ".(  is_readable( $file )?"":"not ")."readable<br/>";
  print "$file is ".(  is_writable( $file )?"":"not ")."writable<br/>";
  print "$file is ".( is_executable( $file )?"":"not")."executable<br/>";
  print "$file is ".( filesize($file))." bytes<br/>";
  print "$file was accessed on " . date( "D d M Y g:i A", fileatime( $file ) )."<br/>";
  print "$file was modified on " . date( "D d M Y g:i A", filemtime( $file ) )."<br/>";
  print "$file was changed on  " . date( "D d M Y g:i A", filectime( $file ) )."<br/>";
}
?>
<html>
<head>
<title>Multiple File Tests</title>
</head>
<body>
<div>
<?php
outputFileTestInfo( "test.txt" );
?>
</div>
</body>
</html>



Is a directory

 
<?
if ( is_dir( "/tmp" ) ) {
  print "/tmp is a directory";
}
?>



is_dir() function verifies that the file is a directory

 
bool is_dir (string filename)
<?
    $isdir = is_dir("index.html"); // returns false
    
    $isdir = is_dir("book"); // returns true
?>



Working with Directories

 
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] . "<br />"; 
                } 
            } 
        } 
    } 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<br />\n";
            }
            closedir($handle);
    }
?>