PHP/File Directory/File Property
Содержание
- 1 Checking the Mode of a File
- 2 Determining the Size of a File
- 3 File accessed time
- 4 File changed time
- 5 filegroup: Gets file group
- 6 File last accessed time
- 7 File last changed time
- 8 File last updated time
- 9 File modified time
- 10 Get file permission
- 11 Get the file owner
- 12 Get the file size: filesize
- 13 Is it a file: is_file()
- 14 Is the file a directory: is_dir()
- 15 Is the file executable: is_executable
- 16 Is the file readable: is_readable()
- 17 Is the file writable: is_writable()
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>