File PHP Buka/Baca/Tutup
Dalam bab ini kami akan mengajari Anda cara membuka, membaca, dan menutup file di server.
PHP Buka File - fopen()
Metode yang lebih baik untuk membuka file adalah dengan fopen()
fungsi. Fungsi ini memberi Anda lebih banyak opsi daripada readfile()
fungsi.
Kami akan menggunakan file teks, "webdictionary.txt", selama pelajaran:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
Parameter pertama fopen()
berisi nama file yang akan dibuka dan parameter kedua menentukan mode file mana yang harus dibuka. Contoh berikut juga menghasilkan pesan jika fungsi fopen() tidak dapat membuka file yang ditentukan:
Contoh
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Tip: Fungsi fread()
dan fclose()
akan dijelaskan di bawah ini.
File dapat dibuka dalam salah satu mode berikut:
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 |
File Baca PHP - fread()
Fungsi fread()
membaca dari file yang terbuka.
Parameter pertama fread()
berisi nama file yang akan dibaca dan parameter kedua menentukan jumlah byte maksimum untuk dibaca.
Kode PHP berikut membaca file "webdictionary.txt" sampai akhir:
fread($myfile,filesize("webdictionary.txt"));
PHP Tutup File - fclose()
Fungsi fclose()
ini digunakan untuk menutup file yang terbuka.
Ini adalah praktik pemrograman yang baik untuk menutup semua file setelah Anda selesai menggunakannya. Anda tidak ingin file terbuka berjalan di server Anda mengambil sumber daya!
fclose()
Membutuhkan nama file (atau variabel yang menyimpan nama file) yang ingin kita tutup :
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP Membaca Baris Tunggal - fgets()
Fungsi fgets()
ini digunakan untuk membaca satu baris dari sebuah file.
Contoh di bawah menampilkan baris pertama file "webdictionary.txt":
Contoh
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Catatan: Setelah panggilan ke fgets()
fungsi, penunjuk file telah pindah ke baris berikutnya.
PHP Periksa End-Of-File - feof()
Fungsi ini feof()
memeriksa apakah "end-of-file" (EOF) telah tercapai.
Fungsi feof()
ini berguna untuk mengulang data yang panjangnya tidak diketahui.
Contoh di bawah membaca file "webdictionary.txt" baris demi baris, hingga akhir file tercapai:
Contoh
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP Membaca Karakter Tunggal - fgetc()
Fungsi fgetc()
ini digunakan untuk membaca satu karakter dari sebuah file.
Contoh di bawah ini membaca file "webdictionary.txt" karakter demi karakter, hingga akhir file tercapai:
Contoh
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
Catatan: Setelah panggilan ke fgetc()
fungsi tersebut, penunjuk file berpindah ke karakter berikutnya.
Referensi Sistem File PHP Lengkap
Untuk referensi lengkap fungsi sistem file, buka Referensi Sistem File PHP lengkap kami .