1. Open and close the file (read the contents of the file, write to the file)
Reading the contents of a file
File_get_contents ()//php5 or more
<? PHP $filename= "./test.txt"; // read the contents of a file Echo file_get_contents ($filename); Echo "<br>"; // writes content to a file, returning the number of bytes written to the data in the file Echo file_put_contents ("./one.txt", "123"); Echo "<br>";? >
File ();
Readfiel ();
Insufficient: Read all, cannot read the specified area
Read partial characters:
fopen (Url,mode)
Mode
R Open File in read-only mode
r+ more than read the way a write
W opens in a write-only manner, if the file does not exist, creates the file, writes the data, and if the file exists, clears the data from the source file and writes
w+ read more than the way he wrote it.
A is opened in a write-only manner, if the file does not exist, the file is created and written to the file, if the original file has content, but does not clear the contents of the file (append)
A + is more readable than the way it is written.
b Open File in binary mode (picture, movie)
T open a file in text mode
Fread ()//read characters of specified length
FGETC ()//read one character at a time from a file
Fgets ()//read one line of characters from a file at a time
Feof ($file); If there is an error reading the file, or to the end of the file, return True
Write file contents
File_put_contents ()//php5 or more
Create and write content if the file does not exist
If the file exists, delete the original contents of the file, and re-write
Insufficient: cannot be written in an additional way
fopen ()
Fwrite () alias Fputs ()
Fwrite (the file's resource (fopen returned), writes the content)
<?PHP//returns the resource if open succeeds, false if failure is returned$file=fopen("./test.txt", "R");//fread () reads a specified length of characters from a fileEcho fread($file, 5). " <br> ";//read one character at a time from a fileEcho fgetc($file)." <br> ";//reads one line of characters from a file at a timeEcho fgets($file);//Close File Resourcefclose($file);//Read remote Files$file=fopen("Http://www.163.com", "R");$str="";//feof (), error reading file or file content read, return to True while(!feof($file)){ $str.=fread($file, 1024);}Echo $str;//Close Filefclose($file);$file=fopen("./test.txt", "W");//write content to a filefwrite($file, "Wwwwwww");fclose($file);//write in Append mode$file=fopen("./test.txt", "a");fwrite($file, "AAAAAAAAA");fclose($file);?>
Local file
./test.txt
c:/appserv/www/index.php
Remote Files
Http://www.baidu.com, etc.
2. File Internal pointers
Ftell (); Returns the location of the file pointer read/write
int fseek (resource $handle, int $offset [, int $whence = Seek_set]) positioned in the file pointer
Whence values are:
Seek_set-The set position equals the offset byte.
Seek_cur-Sets the position to the current position plus offset.
Seek_end-Set the position to the end of the file plus offset.
Rewind (); File pointer back to where the file started
<?PHP$file=fopen("./test.txt", "R");//Ftell (); Returns the location where the file pointer reads and writesEcho Ftell($file)." <br> ";Echo fread($file, 10). " <br> ";Echo Ftell($file)." <br> ";//fseek (); Locating in the file pointerfseek($file,-20,seek_end);Echo Ftell($file)." <br> ";Echo fread($file, 20). " <br> ";//Return file HeaderRewind($file);Echo Ftell($file);fclose($file);?>
A small message board written with file processing
<?php
$mess = "./file.txt";
if (Isset ($_post["sub"])) {
$strmess =$_post["name"]. ' <l> '. $_post["til"]. ' <l> '. $_post["con"]. ' <l> '. Time (). ' <n> ';
Write ($mess, $strmess);
if (file_exists ($mess)) {
$con =read ($mess);
RTrim () Remove the right character in order to separate and remove the last <n>
$con =rtrim ($con, "<n>");
Explode () separates strings with <n>
$rows =explode ("<n>", $con);
foreach ($rows as $row) {
List () The string assigned to the corresponding variable
List ($username, $title, $content, $time) =explode ("<l>", $row);
Echo ' <p> ' $username. ' In '. Date ' ("Y-m-d h:i:s", $time). ' Say ';
echo $title. "";
echo $content. ' </p> ';
}
}
}
Read data from a file
function Read ($fileName) {
//
$file =fopen ($fileName, "R");
Flock (, lock_sh) to file locking (read out data)
if (Flock ($file, lock_sh)) {
read out all the data of the file
$con =fread ($file, FileSize ($fileName));
Release lock
Flock ($file, lock_un);
}
Fclose ($file);
return $con;
}
Write data to a file
function Write ($fileName, $mess) {
Append Write
$file =fopen ($fileName, "a");
Flock () to file locking (write data)
if (Flock ($file, lock_ex)) {
Fwrite ($file, $mess);
Release lock
Flock ($file, lock_un);
}
Fclose ($file);
}
?>
<form action= "8.php" method= "POST" >
Name:<input type= "text" name= "name" ><br>
Title:<input type= "text" name= "til" ><br>
Body:<textarea name= "Con" ></textarea><br>
<input type= "Submit" Name= "sub" value= "Commit" ><br>
</form>
PHP file System processing (ii)