tutorial pengembangan web terbaru
 

PHP 5 Open File / Baca / Tutup


Dalam bab ini kita akan mengajarkan cara untuk membuka, membaca, dan menutup file di server.


PHP Open File - fopen()

Metode yang lebih baik untuk membuka file adalah dengan fopen() function. Fungsi ini memberikan Anda lebih banyak pilihan 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 dari fopen () berisi nama file yang akan dibuka dan parameter kedua menentukan dalam mode yang file harus dibuka. Contoh berikut juga menghasilkan pesan jika fopen () fungsi 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);
?>
Menjalankan contoh »

Tip: The fread () dan fclose () fungsi akan dijelaskan di bawah.

File mungkin dibuka di 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

PHP Baca File - fread ()

The fread () fungsi membaca dari file terbuka.

Parameter pertama fread () berisi nama file untuk membaca dari dan parameter kedua menentukan jumlah maksimal byte untuk membaca.

Berikut kode PHP membaca "webdictionary.txt" file sampai akhir:

fread($myfile,filesize("webdictionary.txt"));

PHP Tutup File - fclose()

The fclose() adalah fungsi yang digunakan untuk menutup file yang terbuka.

Ini adalah praktek pemrograman yang baik untuk menutup semua file setelah Anda selesai dengan mereka. Anda tidak ingin file yang terbuka berlarian di server Anda mengambil sumber daya!

The fclose() membutuhkan nama file (atau variabel yang menyimpan nama file) kami ingin menutup:

<?php
$myfile = fopen("webdictionary.txt", "r") ;
// some code to be executed....
fclose($myfile) ;
?>

PHP Baca Single Line - fgets()

The fgets() adalah fungsi yang digunakan untuk membaca satu baris dari sebuah file.

Contoh di bawah output baris pertama dari "webdictionary.txt" berkas:

Contoh

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Menjalankan contoh »

Note: Setelah panggilan ke fgets() fungsi, file pointer telah pindah ke baris berikutnya.


PHP Periksa End-Of-File - feof()

The feof() cek fungsi jika "end-of-file" (EOF) telah tercapai.

The feof() fungsi berguna untuk perulangan melalui data panjang tidak diketahui.

Contoh di bawah ini membaca "webdictionary.txt" File baris demi baris, sampai akhir-of-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);
?>
Menjalankan contoh »

PHP Baca Tunggal Karakter - fgetc()

The fgetc() adalah fungsi yang digunakan untuk membaca satu karakter dari sebuah file.

Contoh di bawah ini membaca "webdictionary.txt" File karakter demi karakter, sampai akhir-of-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);
?>
Menjalankan contoh »

Note: Setelah panggilan ke fgetc() fungsi, file pointer bergerak ke karakter berikutnya.


Lengkapi PHP Filesystem Referensi

Untuk referensi lengkap fungsi filesystem, pergi ke kami lengkap PHP Filesystem Referensi .