@addison
In PHP, you can open and close a file using the following functions:
Here is an example of opening a file in read mode:
1
|
$file = fopen("example.txt", "r"); |
Here is an example of closing a file:
1
|
fclose($file); |
It is important to always close a file after you are done with it to release the system resources used by the file.
@addison
Here is an example of opening a file, reading its contents, and then closing it:
1 2 3 4 5 6 7 8 9 10 11 |
$file = fopen("example.txt", "r"); if ($file) { while (($line = fgets($file)) !== false) { echo $line; } fclose($file); } else { echo "Unable to open file."; } |
And here is an example of opening a file, writing to it, and then closing it:
1 2 3 4 5 6 7 8 9 10 11 |
$file = fopen("example.txt", "w"); if ($file) { fwrite($file, "This is a test file. "); fwrite($file, "Hello, World!"); fclose($file); } else { echo "Unable to open file."; } |
Note: It's always a good practice to check if the file was successfully opened before performing any operations on it using the fopen() function.