php读取文件内容:
- //**************第一种读取方式*****************************
- header("content-type:text/html;charset=utf-8");
- //文件路径
- $file_path="text.txt";
- //判断是否有这个文件
- if(file_exists($file_path)){
- if($fp=fopen($file_path,"a+")){
- //读取文件
- $conn=fread($fp,filesize($file_path));
- //替换字符串
- $conn=str_replace("\r\n","
",$conn); - echo $conn."
"; - }else{
- echo "文件打不开";
- }
- }else{
- echo "没有这个文件";
- }
- fclose($fp);
- //*******************第二种读取方式***************************
- header("content-type:text/html;charset=utf-8");
- //文件路径
- $file_path="text.txt";
- $conn=file_get_contents($file_path);
- $conn=str_replace("\r\n","
",file_get_contents($file_path)); - echo $conn;
- fclose($fp);
- //******************第三种读取方式,循环读取*****************
- header("content-type:text/html;charset=utf-8");
- //文件路径
- $file_path="text.txt";
- //判断文件是否存在
- if(file_exists($file_path)){
- //判断文件是否能打开
- if($fp=fopen($file_path,"a+")){
- $buffer=1024;
- //边读边判断是否到了文件末尾
- $str="";
- while(!feof($fp)){
- $str.=fread($fp,$buffer);
- }
- }else{
- echo "文件不能打开";
- }
- }else{
- echo "没有这个文件";
- }
- //替换字符
- $str=str_replace("\r\n","
",$str); - echo $str;
- fclose($fp);
- 读取INI配置文件的函数:
- $arr=parse_ini_file("config.ini");
- //返回的是数组
- echo $arr['host']."
"; - echo $arr['username']."
"; - echo $arr['password']."
";
参考链接:
php读取文件内容至字符串并加以处理的代码
学习php读取文件内容的方法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
