PHP文件操作中各函数应用技巧
1.打开文件:fopen("文件名","模式");
模式:
r:只读
r+:读/写
w:只写。打开并清空文件的内容。如果文件不存在,则创建文件。
w+:读/写。打开并清空文件的内容。如果文件不存,则创建文件。
a:追加。打开并向文件的末端进行写操作。如果文件不存在,则创建新文件。
a+:读/追加。通过向文件末端写内容,来保持文件内容。
2.关闭文件:fclose();
例如:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li> <span>$</span><span class="attribute">file</span><span>=</span><span class="attribute-value">fopen</span><span>("test.txt","r"); </span> </li> <li class="alt"><span>//some code be executed </span></li> <li> <span class="attribute">fclose</span><span>=($file); </span> </li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
3.检测是否已达到文件的末端:feof();
例如:if(feof($file)){ echo "end of file";}
4.逐行读取文件:fgets();
例如:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">file</span><span>=</span><span class="attribute-value">fopen</span><span>("test.txt","r"); </span></span></li> <li><span>while(!feof($file)){ </span></li> <li class="alt"> <span>echo fgets($file)."</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">/></span><span>"; </span></span> </li> <li><span>} </span></li> <li class="alt"><span>fclose($file); </span></li> </ol>
5. 逐字符读取文件: fgetc()
6.文件是否存在: file_exists(); 存在则返回true,否则返回false
7.将文件读取到一个数组:$array=file("text.txt"),$array[0]就是第一行文本,依次类推。如果要翻转整个数组,例如:
$arr=array_reverse($array);
则最后一行文本就是$arr[0].
获取数组$array里的行数,则用$num=count($array);
判断文本数据库不为空,则用: if($num>0){}
计算总页数:$total=ceil($num/$pagesize) $pagesize是每页显示的信息数。
计算当前页所显示第一条记录号:$number=($page-1)*$pagesize;
以" "作为分隔符,分隔数组$arr中每第$number个单元的数据,并将这些数据赋予数组$rom
for($i=0;$i
将数据row里的单元数据按顺序赋予括号里的变量
list($ip,$datetime,$name,$email,$home)=$row

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.