Copy Code code as follows:
<?php
/**
* Write Data
* @param [string] $path [File path]
* @param [string] $mode [File open mode]
* @param [string] $data [data]
* @return [BOOL]
*/
function WriteData ($path, $mode, $data) {
$fp = fopen ($path, $mode);
$retries = 0;
$max _retries = 100;
do {
if ($retries > 0) {
Usleep (rand (1, 10000));
}
$retries + 1;
}while (!flock ($FP, LOCK_EX) and $retries <= $max _retries);
if ($retries = = $max _retries) {
return false;
}
Fwrite ($FP, $data. " \ r \ n ");
Flock ($FP, lock_un);
Fclose ($FP);
return true;
}
/**
* Read Data
* @param [string] $path [File path]
* @param [string] $mode [File open mode]
* @return String
*/
function ReadData ($path, $mode) {
$fp = fopen ($path, $mode);
$retries = 0;
$max _retries = 100;
do {
if ($retries > 0) {
Usleep (rand (1, 10000));
}
$retries + 1;
}while (!flock ($FP, lock_sh) and $retries <= $max _retries);
if ($retries = = $max _retries) {
return false;
}
$contents = "";
while (!feof ($fp)) {
$contents. = Fread ($fp, 8192);
}
Flock ($FP, lock_un);
Fclose ($FP);
return $contents;
}
WriteData (' D:/webserver/demo.txt ', ' A + ', ' is a demo ');
echo readdata (' D:/webserver ', ' r+ ');