zoukankan      html  css  js  c++  java
  • PHP文件目录操作

    • 目录操作

      • 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' . ' ');   

    fclose($handle);

    } else {   

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

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

    $content = 'Hello word ' . $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');

  • 相关阅读:
    Go入门笔记-14 EdgeX读取配置文件
    Go入门笔记-13 使用EdgeX日志输出
    Go入门笔记-12 输出unix时间戳
    Go入门笔记-11 Go 获取Linux系统CPU占用率
    htop使用
    Ubuntu子系统默认使用root登录
    函数参数传递数组
    c 'CRTSCTS' undeclared
    c 数组指针使用
    使用SD卡刷OpenWRT后,调整分区大小
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/11141812.html
Copyright © 2011-2022 走看看