zoukankan      html  css  js  c++  java
  • spl处理文件(文件详细信息、文件遍历、查询指定行、写入CSV文件)

    <?php
    /**
     * 文件操作
     */
    
    //常用操作
    $file = new SplFileInfo('D:/workspace/xlyy/spl/test.txt');
    $file_info = array(
        'getATime'     => $file->getATime(),     //最后访问时间
        'getBasename'  => $file->getBasename(),  //获取文件名
        'getCTime'     => $file->getCTime(),     //获取inode修改时间
        'getExtension' => $file->getExtension(), //文件扩展名
        'getFilename'  => $file->getFilename(),  //文件名
        'getGroup'     => $file->getGroup(),     //文件所在的组
        'getInode'     => $file->getInode(),     //获取文件Inode
        'getLinkTarget'=> $file->getLinkTarget(),//获取文件链接目标文件
        'getMtime'     => $file->getMtime(),     //文件最后修改时间
        'getOwner'     => $file->getOwner(),     //获取文件所有者
        'getPath'      => $file->getPath(),      //获取不带文件名的路径
        'getPathInfo'  => $file->getPathInfo(),  //上级路径SplFileInfo对象 
        'getPathname'  => $file->getPathname(),  //全路径
        'getPerms'     => $file->getPerms(),     //权限
        'getRealPath'  => $file->getRealPath(),  //绝对路径
        'getSize'      => $file->getSize(),      //文件大小(字节)
        'getType'      => $file->getType(),      //文件类型 file/dir/link
        'isDir'        => $file->isDir(),        //是否是目录
        'isFile'       => $file->isFile(),       //是否是文件
        'isLink'       => $file->isLink(),       //是否是文件
        'isExecutable' => $file->isExecutable(), //是否可执行
        'isReadable'   => $file->isReadable(),   //是否可读
        'isWritable'   => $file->isWritable(),   //是否可写
    );
    echo '<pre>';
    print_r($file_info);
    echo '</pre>';
    echo '<hr />';
    
    //遍历操作
    try{
        foreach (new SplFileObject('test.txt') as $line){//遍历出文件中的内容
            echo $line;
        }
    }catch (Exception $e){
        echo $e->getMessage();
    }
    echo '<hr />';
    
    //查找指定的行
    try{
        $file = new SplFileObject('test.txt');
        $file->seek(2);
        echo $file->current();
    }catch (Exception $e){
        echo $e->getMessage();
    }
    
    //写入CSV文件
    $list = array(
        array('aaaa','bbbbb','cccccc','ddddddddd'),
        array('123',456,789),
        array('aa','bb'),
    );
    $file = new SplFileObject('file.csv','w');
    foreach ($list as $fields){
        $file->fputcsv($fields);
    }
    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    Window如何查看cpu核数,更改CPU开启的核数?
    Mysql5.6.47开放远程访问(修改远程访问密码)
    CentOS7.6新增或修改SSH端口号的步骤
    虚拟机下安装Centos设置静态ip,并通过桥接连接
    windows下安装mysql5.6.47版本
    微软官方安装介质Windows10系统安装教程
    【测试编码URI的函数】
    【JavaScript函数】
    【JavaScript运算符与表达式】
    【JavaScript声明变量的规则】
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5703210.html
Copyright © 2011-2022 走看看