PHP5文件系统函数可以帮助我们处理文件和目录,它们包括fopen()、fclose()、fread()、fwrite()、file_exists()、unlink()、copy()、rename()、mkdir()等。
fopen()函数
fopen()函数用于打开文件,它的语法如下:
resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
$filename参数表示要打开的文件名,$mode参数表示打开文件的模式,常用的模式有:
- r:只读模式,文件必须存在,不存在则报错。
- r+:读写模式,文件必须存在,不存在则报错。
- w:只写模式,文件不存在则新建,存在则清空文件内容。
- w+:读写模式,文件不存在则新建,存在则清空文件内容。
fclose()函数
fclose()函数用于关闭文件,它的语法如下:
bool fclose ( resource $handle )
$handle参数表示要关闭的文件句柄。
fread()函数
fread()函数用于读取文件内容,它的语法如下:string fread ( resource $handle , int $length )
$handle参数表示要读取的文件句柄,$length参数表示要读取的字节数。
fwrite()函数
fwrite()函数用于写入文件内容,它的语法如下:int fwrite ( resource $handle , string $string [, int $length ] )
$handle参数表示要写入的文件句柄,$string参数表示要写入的字符串,$length参数表示要写入的字节数,可选参数。
file_exists()函数
file_exists()函数用于检查文件是否存在,它的语法如下:bool file_exists ( string $filename )
$filename参数表示要检查的文件名。
unlink()函数
unlink()函数用于删除文件,它的语法如下:bool unlink ( string $filename [, resource $context ] )
$filename参数表示要删除的文件名。
copy()函数
copy()函数用于复制文件,它的语法如下:bool copy ( string $source , string $dest [, resource $context ] )
$source参数表示要复制的源文件,$dest参数表示要复制到的目标文件。
rename()函数
rename()函数用于重命名文件,它的语法如下:bool rename ( string $oldname , string $newname [, resource $context ] )
$oldname参数表示要重命名的源文件,$newname参数表示要重命名为的新文件名。
mkdir()函数
mkdir()函数用于创建目录,它的语法如下:bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
$pathname参数表示要创建的目录路径,$mode参数表示目录的访问权限,$recursive参数表示是否递归创建目录。