PHP中的文件操作函数你绝对不能错过!
PHP是一种服务器脚本语言,常被用于web开发,其文件操作函数非常强大。在实际工作中,我们通常需要与文件进行交互,例如读取文件、写入文件、复制文件、重命名文件等等。在这篇文章中,我们将介绍PHP的主要文件操作函数,旨在帮助PHP开发者更轻松地进行文件操作。
一、文件基础操作
1. fopen() 函数:打开文件并返回指向文件的指针。
函数原型:resource fopen ( string $filename , string $mode )
参数说明:
- $filename:文件名或者URL
- $mode:打开文件的模式
模式 描述
r 只读。文件指针在文件的开头。
r+ 读写。文件指针在文件的开头。
w 只写。打开并清空文件的内容;如果文件不存在,创建文件。
w+ 读写。打开并清空文件的内容;如果文件不存在,创建文件。
a 只写。文件指针在文件的结尾;如果文件不存在,创建文件。
a+ 读写。文件指针在文件的结尾;如果文件不存在,创建文件。
2. fclose() 函数:关闭文件。
函数原型:bool fclose ( resource $handle )
参数说明:
- $handle:打开的文件指针
3. fread() 函数:读取文件。
函数原型:string fread ( resource $handle , int $length )
参数说明:
- $handle:要读取的文件指针
- $length:读取的字节数
4. fgets() 函数:读取一行文件(包括行尾符)。
函数原型:string|false fgets ( resource $handle [, int $length ] )
参数说明:
- $handle:要读取的文件指针
- $length:要读取的字节数
5. fwrite() 函数:写入文件。
函数原型:int fwrite ( resource $handle , string $string [, int $length ] )
参数说明:
- $handle:要写入的文件指针
- $string:要写入的字符串
- $length:可选,要写入的字节数。如果未指定,则将写入整个字符串。
6. file_exists() 函数:检查文件是否存在。
函数原型:bool file_exists ( string $filename )
参数说明:
- $filename:要检查的文件名或路径
7. unlink() 函数:删除文件。
函数原型:bool unlink ( string $filename [, resource $context ] )
参数说明:
- $filename:要删除的文件名或路径
二、高级文件操作
1. fgets() 函数和文件操作符
$handle = fopen("/path/to/file", "r");
while (!feof($handle)) {
$line = fgets($handle);
echo $line;
}
fclose($handle);
等同于:
$handle = fopen("/path/to/file", "r");
while (($line = fgets($handle)) !== false) {
echo $line;
}
fclose($handle);
或者使用文件操作符:
$handle = @fopen("/path/to/file", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line;
}
fclose($handle);
}
2. file() 函数:将整个文件读入数组中。
函数原型:array file ( string $filename [, int $flags = 0 [, resource $context ]] )
参数说明:
- $filename:要读取的文件名或路径
- $flags:可选,可以是 FILE_USE_INCLUDE_PATH(搜索 include_path)或 FILE_IGNORE_NEW_LINES(不要在数组元素的末尾添加换行符)
使用示例:
$lines = file("/path/to/file");
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . htmlspecialchars($line) . "
";
}
3. fread() 函数和文件操作符
$handle = fopen("/path/to/file", "r");
$contents = fread($handle, filesize("/path/to/file"));
fclose($handle);
等同于:
$contents = file_get_contents("/path/to/file");
或者使用文件操作符:
if ($handle = @fopen("/path/to/file", "r")) {
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
}
4. copy() 函数:复制文件。
函数原型:bool copy ( string $source , string $dest [, resource $context ] )
参数说明:
- $source:要复制的源文件名或路径
- $dest:目标文件名或路径
使用示例:
copy('/path/to/source_file', '/path/to/destination_file');
5. rename() 函数:重命名文件或将文件移动到新路径。
函数原型:bool rename ( string $oldname , string $newname [, resource $context ] )
参数说明:
- $oldname:要重命名或移动的文件名或路径。
- $newname:文件的新名称或路径
使用示例:
rename('/path/to/source_file', '/path/to/new_file');
6. opendir() 函数:打开目录,并返回一个目录句柄。
函数原型:resource opendir ( string $path [, resource $context ] )
参数说明:
- $path:要打开的目录路径
7. readdir() 函数:读取目录句柄中的下一个条目。
函数原型:string|false readdir ([ resource $dir_handle ] )
参数说明:
- $dir_handle:可选,目录句柄。如果未指定,则将使用上次成功打开的目录句柄。
使用示例:
if ($handle = opendir('/path/to/directory')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != '.' && $entry != '..') {
echo "$entry
";
}
}
closedir($handle);
}
8. mkdir() 函数:在指定目录中创建新的目录。
函数原型:bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
参数说明:
- $pathname:要创建的目录路径
- $mode:可选,指定用于创建目录的权限。默认值为 0777。
- $recursive:可选,如果设为 true,mkdir() 函数将会递归创建目录。
使用示例:
mkdir('/path/to/new_directory');
9. rmdir() 函数:删除目录。
函数原型:bool rmdir ( string $dirname [, resource $context ] )
参数说明:
- $dirname:要删除的目录路径
使用示例:
rmdir('/path/to/directory');
三、总结
在本文中,我们介绍了PHP中的一些常用文件操作函数,包括打开文件、读取文件、写入文件、复制文件、重命名文件、删除文件、读取目录、创建目录等。它们是PHP开发过程中非常必要和便捷的函数,开发者们可以根据自己的需求选择合适的函数来完成文件操作。当然,在文件处理过程中,我们还要注意文件的安全性和可靠性,特别是涉及到文件上传、修改等操作时,要对文件进行严格的安全检查和验证。
