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
<?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>