Copy code The code is as follows:
**
* Write file
* @param string $file file path
* @param string $str writing content
* @param char $mode writing mode
*/
function writeFile($file,$str,$mode='w')
{
$oldmask = @umask(0);
$fp = @fopen($file,$mode);
@flock($fp, 3);
if(!$fp)
{
Return false;
}
else
{
@fwrite($fp,$str);
@fclose($fp);
@umask( $oldmask);
Return true;
}
}
Extended applications, such as recording the URL content of each request
Copy the code The code is as follows:
function writeGetUrlInfo()
{
// Get the requester’s address, client, requested page and parameters
$requestInformation = $_SERVER['REMOTE_ADDR'].', '.$_SERVER['HTTP_USER_AGENT'].', http://'.$_SERVER['HTTP_HOST'].htmlentities ($_SERVER['PHP_SELF']). '?'.$_SERVER['QUERY_STRING']."n";
$fileName = RootPath.'/log/'.date('Y-m-d').'.log'; //Website root directory RootPath It is in the configuration file define('RootPath', substr(dirname(__FILE__)));
writeFile($fileName, $requestInformation, 'a'); //Indicates appending
}
Use file_put_contents($filename,$data,FILE_APPEND); better
http://www.bkjia.com/PHPjc/321895.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321895.htmlTechArticleCopy code The code is as follows: ** *Write file* @param string $file File path* @param string $str Write content* @param char $mode writing mode*/ function writeFile($file,$str,$mo...