Ec (2); & nbsp; functionread ($ filename, $ filesze) & nbsp ;{& nbsp; & nbsp; $ fpfopen ($ filename, & quot; rb & quot;); Use the fopenrb & nbsp; attribute to read files and use flocklock_sh and lock_un to unlock & nbsp; if ($ fp) & nbsp; {& nbsp; & script ec (2); script
Function read ($ filename, $ filesze)
{
$ Fp = fopen ($ filename, "rb"); // read the file using the fopen rb attribute and unlock it with flock lock_sh and lock_un.
If ($ fp)
{
Flock ($ fp, LOCK_SH );
Clearstatcache ();
$ Filesize = filesize ($ filename );
If ($ filesize> 0)
{
$ Data = fread ($ fp, $ filesize );
} Else
{
$ Data = false;
}
Flock ($ fp, LOCK_UN );
Fclose ($ fp );
Return $ data;
} Else
{
Return false;
}
}
/**
* Use fopen to write files
* @ Param string $ filename
* @ Param string $ contents
* @ Return boolean
*/
Function wirte ($ filename, & $ contents)
{
$ Fp = fopen ($ filename, "wb ");
If ($ fp)
{
Flock ($ fp, LOCK_EX); // lock the file at the same time, only one person can operate
Fwrite ($ fp, $ contents );
Flock ($ fp, LOCK_UN); // Save the data grip to unlock the file and save it
Fclose ($ fp );
Return true;
} Else
{
Return false;
}
}
// Apply 1 read file and 2 Write File
$ Body = '';
If ($ a = 1)
{
Read('a.txt ', 1024 );
}
Else
{
Wirte('a.txt ', $ body );
}
// Output file
Echo $ body;
?>