Web EndMemo

PHP Write to File


<?PHP
$handle = fopen("test.txt","w+");
fwrite($handle,"PHP tutorial");
fwrite($handle, "Write to a file");
fclose($handle);
?>

fopen() modes:
w write only, delete the file content, if file not exist, create it
w+ read and write, delete the file content, if file not exist, create it
a write only, append, if file not exist, create it
a+ read and write, if file not exist, create it

file_put_content() function can write content into a file without openning it:
<?PHP
$file = "test.txt";
file_put_contents($file,"append something", FILE_APPEND);
?>

file_put_contents() flags:
FILE_USE_INCLUDE_PATH search file in the included directory
FILE_APPEND append
LOCK_EX exclusive writing, proventing other people write to the file at the same time