Php file read/write operations-PHP source code

Source: Internet
Author: User
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;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.