Home > Article > Backend Development > php file writing
For example, the file we want to operate is demo.txt
One way is
$file = fopen('demo.txt','a');
$content = 'xxxxxxxx';
fwrite($file ,$content);
fclose($file);
Sometimes files are written and written frequently to prevent writing failures. You can use
flock($file,LOCK_EX);
flock($file,LOCK_UN) ;
Another way
use file_put_contents();
if(file_exists('demo.txt')){
txt' ;
$fcontent = $str."rn";
file_put_contents($fp,$fcontent,FILE_APPEND | LOCK_EX);
}else{
echo 'the file does not exist';
}
It should be noted that the file path to be written is an absolute path, which can be obtained by using __FILE__
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.
The above introduces PHP file writing, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.