Introduction: This time we introduce the system functions related to file operations in PHP. These functions are also very important. The following Tianya still gives detailed examples of the more commonly used ones.
basename — Returns the filename part of the path
dirname — Returns the directory part of the path
string basename ( string $path [, string $suffix ] )
string dirname ( string $path )
Example:
//Tianya PHP Blog http://blog.phpha.com
$path = "/home/httpd/phpha.com/index.php";
echo basename( $path);
echo basename($path, '.php');
echo basename($path, '.xxx');
echo dirname($path);
?>
//Result:
index. php
index
index.php
/home/httpd/phpha.com
Note: If the file name ends with the correct suffix, this part will also be removed.
chgrp — Change the group a file belongs to
chown — Change the owner of a file
chmod — Change the file mode
bool chmod ( string $filename , int $mode )
Example:
chmod('/home/phpha.txt', 0755);
?>
copy — copy file
if(copy('index.php', 'index .php.bak')){
echo 'copy success';
}
?>
//The index.php.bak file survives in the current directory
delete — See unlink or unset
unlink — Delete file
if(unlink('index.php.bak')){
echo 'unlink success';
}
?>
//Deleted index.php.bak
_Disk_free_Space — A available space in the directory
Disk_total_space — Return to the total size of the disk of a directory
DiskFreespace — Disk_free_space alias
& & & lt;? PHP
Below: o Echo Disk_free_space ("C :"), '
';
echo disk_total_space("C:");
//Result: the number of bytes returned
17433419776
32218386432
fopen — Open File or URL
fgets — Read a line from the file pointer
feof — Test whether the file pointer has reached the end of the file
fread — Read a file (safe for binary files)
fwrite — Write to a file (safe for binary files) Binary files)
fclose — Close an open file pointer
//Tianya PHP Blog http://blog.phpha.com
$fp = fopen('hello.txt', 'r'); //Open a file
$n = 1 ;
while(!feof($fp)){
echo $n, ' - ', fgets($fp), '
'; //Read a line and output
$n++;
}
fclose($fp); //Close the file
?>
//Output:
1 - Welcome to my blog:
2 - http://blog.phpha.com
fgetc — from the file pointer Read characters
fgetcsv — Read a line from a file pointer and parse CSV fields
fgetss — Read a line from a file pointer and filter out HTML tags
fputcsv — Format a line into CSV and write to a file pointer
fputs — fwrite Alias of
$fp = fopen('hello.txt', 'r');
while(false !== ($char = fgetc($fp))){
echo $ char, '-';
}
?> -
file_exists — Check if a file or directory exists
if(file_exists('hello.txt')){
echo 'hello.txt exists';
}else{
echo 'hello.txt not exists';
}
?>
//输出:
hello.txt exists
file_get_contents — 将整个文件读入一个字符串
file_put_contents — 将一个字符串写入文件
file — 把整个文件读入一个数组中
if($content = file_get_contents('hello.txt')){
file_put_contents('hello.txt.bak', $content);
}
?>
//相当于copy了一份hello.txt
if($content = file('hello.txt')){
print_r($content);
}
?>
//数组形式,每一行是一个数组成员
Array
(
[0] => Welcome to my blog:
[1] => http://blog.phpha.com
)
fileatime — 取得文件的上次访问时间
filectime — 取得文件的 inode 修改时间
filegroup — 取得文件的组
fileinode — 取得文件的 inode
filemtime — 取得文件修改时间
fileowner — 取得文件的所有者
fileperms — 取得文件的权限
filesize — 取得文件大小
filetype — 取得文件类型
echo fileatime('hello.txt');
echo filectime('hello.txt');
echo filegroup('hello.txt');
echo filemtime('hello.txt');
echo fileowner('hello.txt');
echo substr(sprintf('%o', fileperms('hello.txt')), -4);
echo filesize('hello.txt');
echo filetype('hello.txt');
?>
//输出:
1353329003
1353329003
0
1353330002
0
0666
42
file
flock — Lightweight advisory file locking
fnmatch — Match filenames with patterns
fflush — Output buffered contents to a file
fpassthru — Output all remaining data at the file pointer
fscanf — Format input from a file
fseek — In a file Positioning in the pointer
fstat — Get file information through the open file pointer
ftell — Return the read/write position of the file pointer
ftruncate — Truncate the file to the given length
glob — Find the file path matching the pattern
is_dir — Determine whether the given file name is a directory
is_executable — Determine whether the given file name is executable
is_file — Determine whether the given file name is a normal file
is_link — Determine whether the given file name is a symbolic link
is_readable — Determine whether the given file name is readable
is_uploaded_file — Determine whether the file is uploaded through HTTP POST
is_writable — Determine whether the given file name is writable
is_writeable — Alias of is_writable
Explanation: The above functions are used to determine the file Or whether the directory meets the corresponding conditions, returns TRUE or FALSE.
lchgrp — Changes group ownership of symlink
lchown — Changes user ownership of symlink
link — Create a hard link
linkinfo — Get information about a connection
lstat — Give information about a file or symbolic link
mkdir — Create a new directory
move_uploaded_file — Move an uploaded file to a new location
parse_ini_file — Parse a configuration file
pathinfo — Return file path information
pclose — Close the process file pointer
popen — Open the process file pointer
readfile — Output a file
readlink — Return symbol The target of the connection
realpath — Returns the normalized absolute path name
rename — Renames a file or directory
rewind — Rewinds the position of the file pointer
rmdir — Delete a directory
set_file_buffer — Alias for stream_set_write_buffer
stat — Gives information about a file
symlink — Create a symbolic link
tempnam — Create a file with a unique file name
tmpfile — Create a temporary file
touch — Set the access and modification time of the file
umask — Change the current umask
clearstatcache — Clear the file status cache
Summary: In fact, most of the file operation functions are not used. At the same time, you can see how similar these functions are to Linux commands.
The above is excerpted from the PHP manual [10] – Filesystem file system functions. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!