PHP/File Directory/File Property

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

Checking the Mode of a File

<?php
  $myfile = "./test.txt";
  if (is_readable ($myfile)) {
    echo "The file can be read!", "\n";
  } else {
    echo "The file cannot be read.", "/n";
  }
  
  if (is_writable ($myfile)) {
    echo "The file can be used for writing!", "/n";
  } else {
    echo "The file cannot be used for writing.", "/n";
  }
  if (is_executable ($myfile)) {
    echo "The file is executable!", "\n";
  } else {
    echo "The file is not executable.", "\n";
  }
?>



Determining the Size of a File

<?php
  $myfile = "./test.txt";
  if (file_exists ($myfile)) {
    $checksize=filesize ($myfile);
    echo "$checksize";
  } else {
    echo "The file doesn"t exist!";
  }
?>



File accessed time

<html>
<head>
<title>File accessed time</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f was accessed on ".date( "D d M Y g:i A", fileatime( $f ) )."<br>";
}
?>
</body>
</html>



File changed time

<html>
<head>
<title>File changed time</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f was changed on ".date( "D d M Y g:i A", filectime( $f ) )."<br>";
}
?>
</body>
</html>



filegroup: Gets file group

<?php
   $gid = filegroup("/etc/passwd");
   // Returns "0" on Unix, because root usually has GID of 0.
   echo $gid;
?>



File last accessed time

<?php
   $file = "./text.txt";
   echo "File last accessed: ".date("m-d-y g:i:sa", fileatime($file));
?>



File last changed time

<?php
   $file = "./text.txt";
   echo "File inode last changed: ".date("m-d-y g:i:sa", fileatime($file));
?>



File last updated time

<?php
   $file = "./demoCSV.csv";
   echo "File last updated: ".date("m-d-y g:i:sa", filemtime($file));
?>



File modified time

<html>
<head>
<title>File modified time</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f was modified on ".date( "D d M Y g:i A", filemtime( $f ) )."<br>";
}
?>
</body>
</html>



Get file permission

<?php
   echo substr(base_convert(fileperms("/etc/passwd"), 10, 8), 3);
?>



Get the file owner

<?php
   $uid = fileowner("/etc/passwd");
   // Returns "0" on Linux, as root typically has UID of 0.
   
   echo $uid;
?>



Get the file size: filesize

<html>
<head>
<title>Get the file size: filesize</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(filesize($f))." bytes<br>";
}
?>
</body>
</html>



Is it a file: is_file()

<html>
<head>
<title>Is it a file: is_file()</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(is_file( $f )?"":"not ")."a file<br>";
}
?>
</body>
</html>



Is the file a directory: is_dir()

<html>
<head>
<title>Is the file a directory: is_dir()</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(is_dir( $f )?"":"not ")."a directory<br>";
}
?>
</body>
</html>



Is the file executable: is_executable

<html>
<head>
<title>Is the file executable: is_executable</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(is_executable( $f )?"":"not ")."executable<br>";
}
?>
</body>
</html>



Is the file readable: is_readable()

<html>
<head>
<title>Is the file readable: is_readable()</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(is_readable( $f )?"":"not ")."readable<br>";
}
?>
</body>
</html>



Is the file writable: is_writable()

<html>
<head>
<title>Is the file writable: is_writable()</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(is_writable( $f )?"":"not ")."writable<br>";
}
?>
</body>
</html>