Home > Backend Development > PHP Tutorial > php file writing

php file writing

WBOY
Release: 2016-08-08 09:18:53
Original
1757 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template