• 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!!!
  • 相关阅读:
    裸二分图匹配poj1469
    洛谷——P2038 无线网络发射器选址
    洛谷—— P1041 传染病控制
    洛谷—— P1784 数独
    Vijos——T 1092 全排列
    Vijos—— T 1359 Superprime
    高并发解决方案--负载均衡
    request 发送多层字典
    June 11th 2017 Week 24th Sunday
    June 10th 2017 Week 23rd Saturday
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5703210.html
Copyright © 2020-2023  润新知