首页 技术 正文
技术 2022年11月9日
0 收藏 544 点赞 3,581 浏览 2293 个字
  • 目录操作

    • is_dir ( $path ) 判断当前路径是否为目录 ,返回布尔

    • opendir ( $path ) 打开路径目录,返回资源

    • readdir ( $handle ) 读取当前打开目录下一个文件,同时指针向前移动一位,返回字符串 (文件/目录名)

    • closedir ( $handle ) 关闭当前打开目录 返回布尔

    • getcwd ( ) 获得当前工作目录

    • rmdir 删除目录,删除前必须先删除目录下所有文件和目录

  代码:列出指定目录下所有文件和文件名

function traversal_dir($path, $deep = 0) {   

if (is_dir($path)) {       

$handle = opendir($path);       

while (($file = readdir($handle)) !== false) {           

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

 continue;

            }           

            echo str_repeat('-', 2 * $deep) . $file . '</br>';           

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

                traversal_dir($path . '/' . $file, $deep + 1);

            }

        }

    }

}

traversal_dir('./');

  • 文件操作

    • is_file ( $path ) :判断指定 路径是否为文件

    • file_exists ( $path ) : 检查目录或者文件是否存在

    • fopen ( $file ) :打开文件或者 URL 返回资源

    • fread ( resource $handle , int $length ) : 读取文件,可指定长度

    • fwrite ( resource $handle , string $string [, int $length ] ) : 返回写入字符串大小,如果指定了 length,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况。

    • fgets ( resource $handle [, int $length ] ) : 读取一行文本,length指定一行文本长度

    • fclose ( resource $handle ) : 关闭文件

    • basename ( $path ) : 返回指定路径的文件名部分 返回String

    • dirname ( $path ) : 返回指定路径的目录名部分 返回string

    • 路径部分

    • 操作部分

    • stat 获得文件信息

    • 判断部分

    • filesize ( $path ) 获得文件大小 int

    • filetype ( $path ) 获得文件类型 string (可能值:fifo,char,dir,block,link,file 和 unknown)

    • rename ( string $oldname , string $newname [, resource $context ] ) 重命名或者移动 返回布尔

    • unlink ( $path ) 删除文件 返回布尔

    • file_get_contents 将整个文件读如一个字符串

    • file_put_contents 将一个字符串写入文件

  代码:每执行一次文件,向文件头部追加 Hello word

$path = './hello.txt';

if (!file_exists($path)) {   

$handle = fopen($path, 'w+');   

fwrite($handle, 'Hello word' . '\r\n');   

fclose($handle);

} else {   

$handle = fopen($path, 'r');   

$content = fread($handle, filesize($path));   

$content = 'Hello word \r\n' . $content;   

fclose($handle);   

$handle = fopen($path, 'w');   

fwrite($handle, $content);   

fclose($handle);

}

代码:遍历删除文件夹及文件夹下所有文件

function traversal_delete_dir($path) {   

if (is_dir($path)) {       

$handle = opendir($path);       

while (($file = readdir($handle)) !== false) {           

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

continue;

            }           

            if (is_dir($path . '/' . $file))

            {

                traversal_delete_dir($path . '/' . $file);

            } else {               

            if (unlink($path . '/' . $file))

             {                   

            echo '删除文件' . $file . '成功';

                }

            }

        }       

        closedir($handle);       

        rmdir($path);

    }

}

traversal_delete_dir('./shop_api');

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902