PHP Read and write file operation

Source: Internet
Author: User
Tags flock

function Read ($filename, $filesze)
{
$FP =fopen ($filename, "RB");//Fopen RB property read file and unlock with flock Lock_sh,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;
}
}

/**
* Write files with fopen
* @param string $filename
* @param string $contents
* @return Boolean
*/

function Wirte ($filename,& $contents)
{
$FP =fopen ($filename, "WB");
if ($FP)
{
Flock ($FP, LOCK_EX);//Lock file at the same time, only one person can operate
Fwrite ($fp, $contents);
Flock ($FP, lock_un);//save data grip for unlock file and save
Fclose ($FP);
return true;
}else
{
return false;
}
}

Apply 1 read files, 2 write files
$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.