zoukankan      html  css  js  c++  java
  • PHP读取文件内容的三种方式

    <?php
    // 第一种读取方式
    header("content-type:text/html;charset=utf-8");
    // 文件路径
    $fileA = "A.txt";
    // 判断是否有这个文件
    if (file_exists($fileA))
    {
        if (FALSE!=($fp = fopen($fileA, "a+")))
        {
            //读取文件
            $conn = fread($fp, filesize($fileA));
            // 替换字符串
            $conn = str_replace("rn", "<br/>", $conn);
            echo $conn;
        }
        else
        {
            echo "文件打不开";
        }
    }
    else
    {
        echo "没有这个文件";
    }
    fclose($fp);
    ?>
    View Code
    <?php
        header("content-type:text/html;charset=utf-8");
        //文件路径
        $file_path="A.txt";
        $conn=file_get_contents($file_path);
        $conn=str_replace("rn","<br/>",file_get_contents($file_path));
        echo $conn;
    ?>
        
    <?php
    header ( "content-type:text/html;charset=utf-8" );
    // 文件路径
    $file_path = "A.txt";
    // 判断文件是否存在
    if (file_exists ( $file_path )) 
    {
        // 判断文件是否能打开
        if (FALSE != ($fp = fopen ( $file_path, "a+" ))) 
        {
            $buffer = 1024;
            // 边读边判断是否到了文件末尾
            $str = "";
            while ( ! feof ( $fp ) ) 
            {
                $str .= fread ( $fp, $buffer );
            }
        } 
        else 
        {
            echo "文件不能打开";
        }
    }
    else 
    {
        echo "没有这个文件";
    }
    // 替换字符
    $str = str_replace ( "rn", "<br>", $str );
    echo $str;
    fclose ( $fp );
    ?>
    View Code
  • 相关阅读:
    算法——戳气球(最大乘积和)
    算法——股票买卖问题
    算法——最长上升子序列(DP和二分)
    runtime debug sample
    兼顾站点启动与数据安全性
    SQLServer出发器中使用二进制字段
    OutputCache a2过期时间的设置
    wget 163.com
    SQLServer性能优化
    分页控件设计思路
  • 原文地址:https://www.cnblogs.com/cexm/p/6186652.html
Copyright © 2011-2022 走看看