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!!!
  • 相关阅读:
    linux如何给程序添加自启动
    nginx 反向代理apache服务器 配置java与PHP共存环境
    eclipse配置Js环境spket
    Linux下实现秒级定时任务的两种方案
    Linux时间戳和标准时间的互转
    thinkphp与php共享session
    安装PHP sphinx扩展 sphinx-1.1.0/sphinx.c:105:2: error: too few arguments 错误
    MySQLCouldn't find MySQL manager
    PHP 使用header函数设置HTTP头的示例方法 表头 (xlsx下载)
    JAVA正则表达式 Pattern和Matcher
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5703210.html
Copyright © 2011-2022 走看看