php面试专题---7、文件及目录处理考点

php面试专题---7、文件及目录处理考点

一、总结

一句话总结:

用脑子:基本文件操作和目录操作了解一波,不必强求

1、不断在文件hello.txt头部写入一行“Hello World”字符串,要求代码完整?

|||-begin

<?php
// 打开文件
//
// 将文件的内容读取出来,在开头加入Hello World
//
// 将拼接好的字符串写回到文件当中
//
// Hello 7891234567890
//
$file = './hello.txt';
$handle = fopen($file, 'r');
$content = fread($handle, filesize($file));
$content = 'Hello World'. $content;
fclose($handle);
$handle = fopen($file, 'w');
fwrite($handle, $content);
fclose($handle);

|||-end

不能使用把文件指针移到开头的方式,因为会覆盖

2、php访问远程文件?

开启allow_url_fopen,HTTP协议连接只能使用只读,FTP协议可以使用只读或者只写

3、php目录操作函数?

名称相关:basename()、dirname()、pathinfo()
目录读取:opendir()、readdir()、closedir()、rewinddir()
目录删除:rmdir();目录创建:mkdir()

4、php文件操作其它函数?

文件大小:filesize()
文件拷贝:copy()
删除文件:unlink()
文件类型:filetype()
文件大小:filesize()
目录大小:disk()、free_space()、disk_total_space()
文件拷贝:copy()
删除文件:unlink()


文件类型:filetype()
重命名文件或者目录:rename()
文件截取:ftruncate()
文件属性:file_exists()、is_readable()、is_writable()、is_executable()、filectime()、fileatime()、filemtime()

文件锁:flock()
文件指针:ftell()、fseek()、rewind()

5、通过PHP函数的方式对目录进行遍历,写出程序?

|||-begin

<?php
$dir = './test';
// 打开目录
// 读取目录当中的文件
// 如果文件类型是目录,继续打开目录
// 读取子目录的文件
// 如果文件类型是文件,输出文件名称
// 关闭目录
function loopDir($dir)
{
    $handle = opendir($dir);

    while(false!==($file = readdir($handle)))
    {
        if ($file != '.' && $file != '..')
        {
            echo $file. "\n";
            if (filetype($dir. '/'. $file) == 'dir')
            {
                loopDir($dir. '/'. $file);
            }
        }
    }
}
loopDir($dir);

|||-end

1、while(false!==($file = readdir($handle)))
2、if ($file != '.' && $file != '..')
3、if (filetype($dir. '/'. $file) == 'dir')

二、内容在总结中

 

转载于:https://www.cnblogs.com/Renyi-Fan/p/11061243.html

猜你喜欢

转载自blog.csdn.net/weixin_33894992/article/details/93572347
今日推荐