PHP/File Directory/File Property

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

Checking the Mode of a File

   <source lang="html4strict">

<?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";
 }

?>

      </source>
   
  


Determining the Size of a File

   <source lang="html4strict">

<?php

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

?>

      </source>
   
  


File accessed time

   <source lang="html4strict">

<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
"; return; } print "$f was accessed on ".date( "D d M Y g:i A", fileatime( $f ) )."
";

} ?> </body> </html>

      </source>
   
  


File changed time

   <source lang="html4strict">

<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
"; return; } print "$f was changed on ".date( "D d M Y g:i A", filectime( $f ) )."
";

} ?> </body> </html>

      </source>
   
  


filegroup: Gets file group

   <source lang="html4strict">

<?php

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

?>


      </source>
   
  


File last accessed time

   <source lang="html4strict">

<?php

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

?>


      </source>
   
  


File last changed time

   <source lang="html4strict">

<?php

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

?>


      </source>
   
  


File last updated time

   <source lang="html4strict">

<?php

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

?>

      </source>
   
  


File modified time

   <source lang="html4strict">

<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
"; return; } print "$f was modified on ".date( "D d M Y g:i A", filemtime( $f ) )."
";

} ?> </body> </html>

      </source>
   
  


Get file permission

   <source lang="html4strict">

<?php

  echo substr(base_convert(fileperms("/etc/passwd"), 10, 8), 3);

?>


      </source>
   
  


Get the file owner

   <source lang="html4strict">

<?php

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

?>

      </source>
   
  


Get the file size: filesize

   <source lang="html4strict">

<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
"; return; } print "$f is ".(filesize($f))." bytes
";

} ?> </body> </html>

      </source>
   
  


Is it a file: is_file()

   <source lang="html4strict">

<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
"; return; } print "$f is ".(is_file( $f )?"":"not ")."a file
";

} ?> </body> </html>

      </source>
   
  


Is the file a directory: is_dir()

   <source lang="html4strict">

<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
"; return; } print "$f is ".(is_dir( $f )?"":"not ")."a directory
";

} ?> </body> </html>

      </source>
   
  


Is the file executable: is_executable

   <source lang="html4strict">

<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
"; return; } print "$f is ".(is_executable( $f )?"":"not ")."executable
";

} ?> </body> </html>

      </source>
   
  


Is the file readable: is_readable()

   <source lang="html4strict">

<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
"; return; } print "$f is ".(is_readable( $f )?"":"not ")."readable
";

} ?> </body> </html>

      </source>
   
  


Is the file writable: is_writable()

   <source lang="html4strict">

<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
"; return; } print "$f is ".(is_writable( $f )?"":"not ")."writable
";

} ?> </body> </html>

      </source>