-
Notifications
You must be signed in to change notification settings - Fork 0
/
27 File Open Read Close.php
47 lines (43 loc) · 1.94 KB
/
27 File Open Read Close.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!-- PHP File Open/Read/Close -->
<title>PHP File Open/Read/Close</title>
<!--
The file may be opened in one of the following modes : -
Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
-->
<?php
// PHP Open File - fopen()
$filePointer = fopen("PHP Testing/Notes.txt", "r") or die("Unable To Open File !");
// PHP Read File - fread()
echo fread($filePointer, filesize("PHP Testing/Notes.txt"));
// PHP Close File - fclose()
fclose($filePointer);
?>
<hr>
<!-- PHP Read Single Line - fgets() -->
<?php
$filePointer = fopen("PHP Testing/Notes.txt", "r") or die("Unable To Open File !");
#PHP Check End-Of-File - feof()
while (!feof($filePointer)) {
echo fgets($filePointer) . "<br>";
}
fclose($filePointer);
?>
<hr>
<!-- PHP Read Single Character - fgetc() -->
<?php
$filePointer = fopen("PHP Testing/Notes.txt", "r") or die("Unable To Open File !");
#PHP Check End-Of-File - feof()
while (!feof($filePointer)) {
echo fgetc($filePointer) . " ";
}
fclose($filePointer);
?>
<hr>