php ulink,PHP 文件处理函数详解

导语

PHP可以很方便的对目录、文件进行操作,包括创建、读取、修改、删除等。

mkdir

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )

尝试新建一个由 pathname 指定的目录。

mkdir 可以创建 pathname 指定目录,默认 mode 是0777,在 windows 下被忽略,失败返回 false。

mkdir('./test');// 在当前目录创建 test 目录

rmdir

bool rmdir ( string $dirname [, resource $context ] )

尝试删除 dirname 所指定的目录。 该目录必须是空的,而且要有相应的权限。 失败时会产生一个 E_WARNING 级别的错误。

如上所示,rmdir 可以删除目录,需要注意的是该目录必须为空,而且要有权限,失败返回 false。示例

rmdir('./test');// 删除当前目录下的 test 目录。

file_put_contents

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

和依次调用 fopen(),fwrite() 以及 fclose() 功能一样。

file_put_contents 将 data 写入 filename 文件中,如果没有此文件,则创建,失败返回 false,成功返回写入字节数。示例

file_put_contents('./test.txt', date('Y-m-d H:i:s'));// 在当前目录创建 test.txt,并且写入数据

file_put_contents('./test.txt', date('Y-m-d H:i:s'), FILE_APPEND);// 在 test.txt 文件中,追加数据

file_get_contents

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。

file_get_cntents 读取 filename 中的内容,返回字符串,失败返回 false。示例

echo file_get_contents('./test.txt');// 输出当前目录下 test.txt 的内容

unlink

bool unlink ( string $filename [, resource $context ] )

删除 filename。和 Unix C 的 unlink() 函数相似。 发生错误时会产生一个 E_WARNING 级别的错误。

unlink 删除 filename 文件,同样需要注意权限。示例

unlink('./test.txt');// 删除当前目录的 test.txt

rename

bool rename ( string $oldname , string $newname [, resource $context ] )

尝试把 oldname 重命名为 newname。

rename 不仅可以文件重命名,还可以移动文件,失败返回 false。示例

rename('./test.txt', './test1.txt');// 当前目录下的 test.txt 重命名为 test1.txt

rename('./test1.txt', './test/test.txt');// 将当前目录下的 test1.txt, 移动到 test下

copy

bool copy ( string $source , string $dest [, resource $context ] )

将文件从 source 拷贝到 dest。

如上所示,失败返回 false。示例

copy('./test/test.txt', './test1.txt');// test 目录下的test.txt, 拷贝到当前目录

实例及注释

下面是几个实例,工作或面试中会用到。

/**

* 遍历目录下所有文件

* @param $path

*/

function getAllFiles($path)

{

if (!is_dir($path)) {

exit('错误');

}

echo '

  • ';

foreach (scandir($path) as $line) {

if ($line == '.' || $line == '..') {

continue;

}

if (is_dir($path . '/' . $line)) {

getAllFiles($path . '/' . $line);

}

echo '

' . $path . '/' . $line . '';

}

echo '

';

}

/**

* 删除目录下所有文件

* @param $path

*/

function delAllFile($path)

{

if (!is_dir($path)) {

exit('目录不存在');

}

$dir = opendir($path);

while ($filename = readdir($dir)) {

if ($filename != "." && $filename != "..") {

$file = $path . "/" . $filename;

if (is_dir($file)) {

delAllFile($file);

} else {

unlink($file);

}

}

}

closedir($dir);

rmdir($path);

}

注释

mkdir de 中的 recursive 参数,可以嵌套创建目录;

file_put_contents 中的 flags 参数,可以进行组合,详情参考链接;

file_put_contents 也可能返回等同于 false 的非布尔值,使用===判断;

file_get_contents 也可以打开 URL,获取网页内容;

file_get_contents 如果要打开有特殊字符的 URL (比如说有空格),就需要使用 urlencode() 进行 URL 编码;

copy 如果目标文件已存在,将会被覆盖;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值