A Directory Operations
BaseName--Returns the file name portion of the path
DirName--Returns the directory portion of the path
PathInfo--Returns information about the file path
Realpath--Returns the normalized absolute path name
Copy CodeThe code is as follows:
$path = ' demo1.php ';
$path = Realpath ($path);
Echo basename ($path);
Echo '
';
echo dirname ($path);
Echo '
';
$array _path = PathInfo ($path);
Echo ' basename: '. $array _path[' basename ']. '
';
Echo ' dirname: '. $array _path[' dirname ']. '
';
echo ' extension: '. $array _path[' extension ']. '
';
echo ' filename: '. $array _path[' filename '. '
';
?>
Output:
demo1.php
D:\AppServ\www\Basic6
basename:demo1.php
Dirname:d:\appserv\www\basic6
extension:php
Filename:demo1
Two Disk, directory, and file count
1. View file size and disk space
FileSize--Get file size
Disk_free_space--Returns the free space in the directory
Disk_total_space--Returns the total disk size of a directory
Copy CodeThe code is as follows:
$path = ' demo2.php ';
$path = Realpath ($path);
$drive = ' C: ';
Echo Round (FileSize ($path)/1024,2). ' KB '. '
';
Echo Round (Disk_free_space ($drive)/1024/1024/1024,2). ' GB '. '
';
Echo Round (Disk_total_space ($drive)/1024/1024/1024,2). ' GB '. '
';
?>
Output
0.26kb
10.61GB
30.01GB
2. Various time to obtain documents
Fileatime--Get the last access time of the file
Filectime--Get the Inode modification time of the file
Filemtime--Get File modification time
Copy CodeThe code is as follows:
$file = Realpath ('.. /basic5/demo3.php ');
$date _format = ' y-m-d h:i:s ';
Echo ' Lastest accessing time: '. Date ($date _format, Fileatime ($file)). '
';
Echo ' Lastest change time: '. Date ($date _format, Filectime ($file)). '
';
Echo ' Lastest Modify time: '. Date ($date _format, Filemtime ($file)). '
';
?>
Output
Lastest accessing time:2011-12-18 04:26:49
Lastest Change time:2011-12-18 04:26:49
Lastest Modify time:2011-12-18 04:29:15
Three File processing
Two ways to read and write files:
1.php all versions are supported by the method:
fopen--Open file or URL
Fclose--Close an open file pointer
Fwrite-Write file (can be used safely for binary files)
Table 1. List of possible values for mode in fopen ()
Mode |
Description |
' R ' |
Read-only opens, pointing the file pointer to the file header. |
' R+ ' |
Read-write mode opens, pointing the file pointer to the file header. |
' W ' |
The Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it. |
' w+ ' |
Read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it. |
A |
Write to open, pointing the file pointer to the end of the file. If the file does not exist, try to create it. |
' A + ' |
Read-write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try to create it. |
' X ' |
Creates and opens in writing, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an e_warning level error message. If the file does not exist, try to create it. This specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used on local files. |
' x+ ' |
Creates and opens read-write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an e_warning level error message. If the file does not exist, try to create it. This specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used on local files. |
Copy CodeThe code is as follows:
$fp = fopen (' file1.txt ', ' W ');
$OUTSTR = "My name is Anllin,\r\nmy 29.";
Fwrite ($fp, $outStr, strlen ($OUTSTR));
Fclose ($FP);
?>
Output
My name is Anllin,
My age is 29.
2.PHP5 new ways to join
File_put_contents--Writes a string to the file
Copy CodeThe code is as follows:
File_put_contents (' File2.txt ', "My name is Anllin,\r\nmy age is 29.");
?>
Output
My name is Anllin,
My age is 29.
How to read the contents of a file:
Function |
Function |
FGETC () |
Reads a character and moves the pointer to the next character |
Fgets () |
Reads a line of characters, and you can specify the length of a line display. |
FGETSS () |
Reads a row from the file pointer and filters out HTML tags |
Fread () |
Read the quantitative characters |
Fpassthru () |
Output file to all remaining data at the specified point |
File () |
Reads the entire file into an array, grouped by rows |
Readfile () |
Read in a file and write to the output buffer |
File_get_contents () |
Reads the entire file into a string |
Feof () |
Judging the function of reading the file |
File_exists () |
To see if a file exists |
The contents of the sample file File1.txt are as follows:
My name is Anllin,
My age is 29.
FGETC--reading characters from the file pointer
demo.php
Copy CodeThe code is as follows:
$fp = fopen (' file1.txt ', ' R ');
echo fgetc ($FP);
echo fgetc ($FP);
Fclose ($FP);
?>
Output:
My
Fgets--Reads a line from the file pointer
Copy CodeThe code is as follows:
$fp = fopen (' file1.txt ', ' R ');
Echo fgets ($FP);
Echo fgets ($FP);
Fclose ($FP);
?>
Output
My name is Anllin, my 29.
FGETSS--Reads a line from the file pointer and filters out HTML tags
Copy CodeThe code is as follows:
$fp = fopen (' File3.txt ', ' W ');
$OUTSTR = "My name is
Anllin";
Fwrite ($fp, $outStr, strlen ($OUTSTR));
Fclose ($FP);
$ftp = fopen (' file3.txt ', ' R ');
echo Fgetss ($FTP);
Fclose ($FTP);
?>
Output
My name is Anllin
Fread--read files (can be used safely in binary files)
Copy CodeThe code is as follows:
$filename = ' file1.txt ';
$fp = fopen ($filename, ' R ');
$contents = Fread ($fp, FileSize ($filename));
Echo $contents;
Fclose ($FP);
?>
Output
My name is Anllin, my 29.
Fpassthru--All remaining data at the output file pointer
Copy CodeThe code is as follows:
$filename = ' file1.txt ';
$fp = fopen ($filename, ' RB ');
$leftSize = Fpassthru ($FP);
Echo $leftSize;
Fclose ($FP);
?>
Output
My name is Anllin, my 29. 33
FILE--Reads the entire document into an array
Copy CodeThe code is as follows:
$lines = File (' file1.txt ');
foreach ($lines as $line _num = $line)
{
echo $line _num. ': ' $line. '
';
}
?>
Output
0:my name is Anllin,
1:my is 29.
ReadFile--Output a file
Copy CodeThe code is as follows:
$size = ReadFile (' file1.txt ');
Echo $size;
?>
Output
My name is Anllin, my 29.33
File_get_contents--Reads the entire file into a string (php5.0 new)
Copy CodeThe code is as follows:
$contents = file_get_contents (' file1.txt ');
Echo $contents;
?>
Output
My name is Anllin, my 29.
Feof--Test whether the file pointer is to the end of the file location
Copy CodeThe code is as follows:
$fp = fopen (' file1.txt ', ' R ');
while (!feof ($FP))
{
echo fgetc ($FP);
}
Fclose ($FP);
?>
Output
My name is Anllin, my 29.
File_exists--Check whether a file or directory exists
Copy CodeThe code is as follows:
$filename = ' file1.txt ';
if (file_exists ($filename))
{
Echo ' perform file read and write operations ';
}
Else
{
Echo ' The file you are looking for does not exist ';
}
?>
Output
Perform file read and write operations
FileSize--Get file size
Copy CodeThe code is as follows:
$file _size = filesize (' file1.txt ');
echo $file _size;
?>
Output
33
Unlink--Deleting files
Copy CodeThe code is as follows:
$isDeleted = unlink (' File3.txt ');
Echo $isDeleted;
?>
Output
1
Rewind--Rewind the position of the file pointer
Ftell--Returns the position of the file pointer read/write
Fseek--Locating in the file pointer
Copy CodeThe code is as follows:
$fp = fopen (' file1.txt ', ' R ');
Fgetc ($FP);
Fgetc ($FP);
Echo Ftell ($FP). '
';
Rewind ($FP);
Echo Ftell ($FP). '
';
Fgetc ($FP);
Fgetc ($FP);
Echo Ftell ($FP). '
';
Fseek ($fp, 0);//same as Rewind ($FP)
Echo Ftell ($FP). '
';
?>
Output
2
0
2
0
Flock Value of the operation
Action Value |
Significance |
Lock_sh (formerly 1) |
Read/write lock. This means that files can be shared and other people can read the file |
LOCK_EX (formerly 2) |
Write Operation Lock. This is mutually exclusive and the file cannot be shared |
Lock_un (formerly 3) |
Release existing Locks |
LOCK_NB (formerly 4) |
Prevent blocking when requesting locking |
Flock--Light consultation document lock
Copy CodeThe code is as follows:
$filename = ' file1.txt ';
$fp = fopen ($filename, ' RB ');
Flock ($FP, lock_ex);//locked
$contents = Fread ($fp, FileSize ($filename));
Flock ($FP, lock_un);//unlocked
Echo $contents;
Fclose ($FP);
?>
Output
My name is Anllin, my 29.
Directory handle operation
Opendir--Open the directory handle
Readdir--Reading entries from the directory handle
Closedir--Close the directory handle
Copy CodeThe code is as follows:
$dir = Opendir ('.. /basic6 ');
while (!! $file = Readdir ($dir))
{
echo $file. '
';
}
Closedir ($dir);
?>
Output
.
..
. BuildPath
. Project
. settings
demo1.php
demo10.php
demo11.php
demo12.php
demo13.php
demo14.php
demo15.php
demo16.php
demo17.php
demo18.php
demo19.php
demo2.php
demo20.php
demo3.php
demo4.php
demo5.php
demo6.php
demo7.php
demo8.php
demo9.php
File1.txt
File2.txt
Scandir--Lists files and directories in the specified path
Copy CodeThe code is as follows:
$files = Scandir ('.. /basic6 ');
foreach ($files as $file)
{
echo $file. '
';
}
?>
Output
.
..
. BuildPath
. Project
. settings
demo1.php
demo10.php
demo11.php
demo12.php
demo13.php
demo14.php
demo15.php
demo16.php
demo17.php
demo18.php
demo19.php
demo2.php
demo20.php
demo21.php
demo3.php
demo4.php
demo5.php
demo6.php
demo7.php
demo8.php
demo9.php
File1.txt
File2.txt
Rename--Rename a file or directory
Copy CodeThe code is as follows:
Rename (' demo1.php ', ' demo01.php ');
if (file_exists (' demo01.php '))
{
echo ' File rename success ';
}
Else
{
echo ' file rename fail ';
}
?>
Output
File Rename Success
RmDir--Delete directory
Copy CodeThe code is as follows:
RmDir (' 123 ');
if (file_exists (' 123 '))
{
Echo ' delete file fail ';
}
{
Echo ' Delete file Success ';
}
?>
Output
Delete file success
http://www.bkjia.com/PHPjc/324803.html www.bkjia.com true http://www.bkjia.com/PHPjc/324803.html techarticle A. Directory Operation BaseName--Returns the filename part of the path dirname--Returns the directory part of the path PathInfo--Returns the file path information Realpath--Returns the normalized absolute ...