Home  >  Article  >  Backend Development  >  Application skills of various functions in PHP file operations_PHP tutorial

Application skills of various functions in PHP file operations_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:34:53843browse

1. Open file: fopen("file name", "mode");

Mode:

r :read-only
r+:read/write
w:write-only. Open and clear the contents of the file. If the file does not exist, the file is created.
w+: read/write. Open and clear the contents of the file. If the file does not exist, create the file.
a: Append. Open and write to the end of the file. If the file does not exist, a new file is created.
a+: read/append. Maintain file contents by writing to the end of the file.

2. Close the file: fclose();

For example:

<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></span></li></ol>

3. Check whether the end of the file has been reached: feof( );

For example: if(feof($file)){ echo "end of file";}

4. Read the file line by line: fgets();

For example:

<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><span class="tag-name">br</span><span class="tag">/></span><span>";  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>fclose($file); </span></li>
</ol>

5. Read the file character by character: fgetc()

6. Whether the file exists: file_exists(); returns true if it exists, Otherwise, return false

7. Read the file into an array: $array=file("text.txt"), $array[0] is the first line of text, and so on. If you want to flip the entire array, for example:

$arr=array_reverse($array);

The last line of text is $arr[0].

Get the array $array If the number of rows in the text database is $num=count($array);

to determine if the text database is not empty, use: if($num>0){}

to calculate the total pages Number: $total=ceil($num/$pagesize) $pagesize is the number of information displayed on each page.

Calculate the first record number displayed on the current page: $number=($page-1)*$pagesize;

Use " " as the separator to separate each record in the array $arr $number units of data, and assign these data to the array $rom

for($i=0;$i<=$pagesize-1;$i++){$row=explode(" ",$ arr[$number];}

Assign the unit data in the data row to the variables in the brackets in order

list($ip,$datetime,$name,$email,$home) =$row


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445959.htmlTechArticle1. Open file: fopen (file name, mode); Mode: r: read-only r+: read/write w: Write only. Open and clear the contents of the file. If the file does not exist, create the file. w+: read/write. Open and clear...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn