zoukankan      html  css  js  c++  java
  • php 文件操作

    /*php读取文件*/
            $fileName = 'D:/workspace/zhiliao/file.txt';
            /**
             * 打开文件
             * r  只读 指针指向文件头
             * r+ 读写 指针指向文件头
             * w  写入 清空文件内容 文件不存在则创建
             * w+ 读写 清空文件内容 文件不存在则创建
             * a  写入 文件末尾写 文件不存在则创建
             * a+ 读写 文件末尾写 文件不存在则创建
             * x  写入 创建新文件
             * x+ 读写 创建新文件
             * t 默认 文本文件
             * b 二进制文件
             */
            $handle = fopen($fileName,"r");
            /*按 读取本地文件*/
            $fileSize = filesize($fileName);/*文件大小 字节 只可以是本地文件*/
            $contents = fread($handle,$fileSize/2);/*根据文件大小读取文件内容(读取一半内容)*/
            /*可以用来读取远程文件 如 $fileName='http://www.baidu.com'*/
            $content = '';
            while(!feof($handle)){/*feof测试文件指针是否到了文件结束位置*/
                $content .= fread($handle, 1);
            }
            /*按行读取*/
            while (!feof($handle)){
                echo fgets($handle,1024);/*如果一行大于1024则剩余的算下一行*/
            }
            /*按行读取 去掉php和html标签 第三个参数表示不去年的标签*/
            while(!feof($handle)){
                echo fgetss($handle, 1024, '<br>');
            }
            /*按行读取 内容放到数组里*/
            $array = file($fileName);
            /*读取文件内容写入到缓冲区 返回文件大小*/
            $size = readfile($fileName);
            /*读取文件内容到字符串 性能比较好优先使用该方法 但可能不如readfile*/
            $content = file_get_contents($fileName);
            /*当前指针到文件结尾的内容写到缓冲*/
            $a = fseek($handle,10);/*指针定位到10字节处*/
            $b = fpassthru($handle);/*返回文件大小*/
            fclose($handle);
    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    hdu 6702 ^&^ 位运算
    hdu 6709 Fishing Master 贪心
    hdu 6704 K-th occurrence 二分 ST表 后缀数组 主席树
    hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列 LCIS
    hdu 5909 Tree Cutting FWT
    luogu P1588 丢失的牛 宽搜
    luogu P1003 铺地毯
    luogu P1104 生日
    luogu P1094 纪念品分组
    luogu P1093 奖学金
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/13746106.html
Copyright © 2011-2022 走看看