Situatie
The fwrite
Function.
First and foremost is the fwrite
function, which allows you to write string contents to the file stream referenced by the file handle. Let’s go through the following example to understand how it works.
Solutie
<?php
$file_handle
=
fopen
(
'C:\Users\flcri\Desktop\Solutii\3\test.txt'
,
'a+'
);
fwrite(
$file_handle
,
'Test'
);
fwrite(
$file_handle
,
"\n"
);
fwrite(
$file_handle
,
'test2'
);
fclose(
$file_handle
);
?>
First, we’ve opened the C:\Users\flcri\Desktop\Solutii\3\test.txt
file with the a+
mode, which opens it for reading and writing, with the file pointer placed at the end of the file. Thus, our content will be appended to the end of the file, after any other contents. Next, we’ve used the fwrite
function to write a string.
The first argument of the fwrite
function is the file system pointer returned by fopen
—this is how fwrite
knows where to write into. And the second argument is a string which we want to write into a file. As you can see in the above example, you can use the fwrite
function multiple times to write a series of strings before you close the file.
Finally, we’ve used the fclose
function to close the file. It takes only one argument, the file pointer that you want to close. It’s always a good practice to close files by using the fclose
function once you’ve finished with your file operations.
Leave A Comment?