Tutorial PHP

RUMAH PHP Pengenalan PHP Instal PHP Sintaks PHP Komentar PHP Variabel PHP PHP Gema / Cetak Tipe Data PHP String PHP Nomor PHP PHP Matematika Konstanta PHP Operator PHP PHP Jika...Lain...Elseif Beralih PHP PHP Loop Fungsi PHP Array PHP PHP Superglobal PHP RegEx

Formulir PHP

Penanganan Formulir PHP Validasi Formulir PHP Diperlukan Formulir PHP URL/Email Formulir PHP Formulir PHP Lengkap

PHP Lanjutan

Tanggal dan Waktu PHP PHP Termasuk Penanganan File PHP File PHP Buka/Baca Membuat/Menulis File PHP Unggah File PHP Cookie PHP Sesi PHP Filter PHP Filter PHP Tingkat Lanjut Fungsi Panggilan Balik PHP PHP JSON Pengecualian PHP

PHP OOP

PHP Apa itu OOP Kelas/Objek PHP Konstruktor PHP Penghancur PHP Pengubah Akses PHP Warisan PHP Konstanta PHP Kelas Abstrak PHP Antarmuka PHP Ciri-ciri PHP Metode Statis PHP Properti Statis PHP Ruang Nama PHP PHP Iterable

Database MySQL

Database MySQL Koneksi MySQL MySQL Buat DB MySQL Buat Tabel MySQL Sisipkan Data MySQL Dapatkan ID Terakhir MySQL Sisipkan Beberapa MySQL Disiapkan MySQL Pilih Data MySQL Dimana MySQL Dipesan Oleh MySQL Hapus Data Data Pembaruan MySQL Data Batas MySQL

PHP XML

PHP XML Parser PHP SimpleXML Parser PHP SimpleXML - Dapatkan PHP XML Ekspat PHP XML DOM

PHP - AJAX

Pengenalan AJAX AJAX PHP Basis Data AJAX AJAX XML Pencarian Langsung AJAX Jajak Pendapat AJAX

Contoh PHP

Contoh PHP Kompilator PHP Kuis PHP Latihan PHP Sertifikat PHP

Referensi PHP

Ikhtisar PHP Array PHP Kalender PHP Tanggal PHP Direktori PHP Kesalahan PHP Pengecualian PHP Sistem File PHP Filter PHP PHP FTP PHP JSON Kata Kunci PHP PHP Libxml Surat PHP PHP Matematika PHP Lain-lain PHP MySQLi Jaringan PHP Kontrol Keluaran PHP PHP RegEx PHP SimpleXML Aliran PHP String PHP Penanganan Variabel PHP PHP XML Parser PHP Zip Zona Waktu PHP

PHP fopen() Fungsi

Referensi Sistem File PHP

Contoh

Buka file, baca baris - hingga EOF tercapai:

<?php
$file = fopen("test.txt", "r");

//Output lines until EOF is reached
while(! feof($file)) {
  $line = fgets($file);
  echo $line. "<br>";
}

fclose($file);
?>

Definisi dan Penggunaan

Fungsi fopen() membuka file atau URL.

Catatan: Saat menulis ke file teks, pastikan untuk menggunakan karakter akhir baris yang benar! Sistem Unix menggunakan \n, sistem Windows menggunakan \r\n, dan sistem Macintosh menggunakan \r sebagai karakter akhir baris. Windows menawarkan tanda terjemahan ('t') yang akan menerjemahkan \n ke \r\n saat bekerja dengan berkas. Anda juga dapat menggunakan 'b' untuk memaksa mode biner. Untuk menggunakan tanda ini, tentukan 'b' atau 't' sebagai karakter terakhir dari parameter mode.

Sintaksis

fopen(filename, mode, include_path, context)

Nilai Parameter

Parameter Description
filename Required. Specifies the file or URL to open
mode Required. Specifies the type of access you require to the file/stream.

Possible values:

  • "r" - Read only. Starts at the beginning of the file
  • "r+" - Read/Write. Starts at the beginning of the file
  • "w" - Write only. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "w+" - Read/Write. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "a" - Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist
  • "a+" - Read/Write. Preserves file content by writing to the end of the file
  • "x" - Write only. Creates a new file. Returns FALSE and an error if file already exists
  • "x+" - Read/Write. Creates a new file. Returns FALSE and an error if file already exists
  • "c" - Write only. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "c+" - Read/Write. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "e" - Only available in PHP compiled on POSIX.1-2008 conform systems.
include_path Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well
context Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream


Detail Teknis

Nilai Kembali: Sumber daya penunjuk file saat berhasil, FALSE, dan kesalahan saat gagal. Anda dapat menyembunyikan kesalahan dengan menambahkan "@" di depan nama fungsi.
Versi PHP: 4.3+
Log Perubahan PHP: PHP 7.1: Menambahkan opsi "e"
PHP 5.2: Menambahkan opsi "c" dan "c+"

Referensi Sistem File PHP