Pertemuan 08 - PHP Files

3
Aplikasi Web Pertemuan-8 PHP Files Manipulasi file dalam PHP diperlukan untuk membuat, mengupload dan mengedit files. Pada bab ini akan mempelajari bagaimana membuat, membuka dan menutup file. Fungsi fopen Bentuk pemanggilan fungsi ini adalah sebagai berikut : fopen(nama_berkas,mode) nama_berkas : string yang menyatakan nama berkas yang akan dibuka mode : mode yang digunakan untuk membuka berkas Table 1. A list of possible modes for fopen() using mode mode Description 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.

description

oke

Transcript of Pertemuan 08 - PHP Files

  • Aplikasi WebPertemuan-8

    PHP Files

    Manipulasi file dalam PHP diperlukan untuk membuat, mengupload dan mengedit files.Pada bab ini akan mempelajari bagaimana membuat, membuka dan menutup file.Fungsi fopenBentuk pemanggilan fungsi ini adalah sebagai berikut :

    fopen(nama_berkas,mode) nama_berkas : string yang menyatakan nama berkas yang akan dibuka mode : mode yang digunakan untuk membuka berkas

    Table 1. A list of possible modes for fopen() using mode mode Description

    'r' Open for reading only; place the file pointer at the beginning of the file.

    'r+'Open for reading and writing; place the file pointer at the beginning of thefile.

    'w'Open for writing only; place the file pointer at the beginning of the file andtruncate the file to zero length. If the file does not exist, attempt to create it.

    'w+'Open for reading and writing; place the file pointer at the beginning of the fileand truncate the file to zero length. If the file does not exist, attempt to createit.

    'a'Open for writing only; place the file pointer at the end of the file. If the filedoes not exist, attempt to create it.

    'a+'Open for reading and writing; place the file pointer at the end of the file. If thefile does not exist, attempt to create it.

    'x'

    Create and open for writing only; place the file pointer at the beginning of thefile. If the file already exists, the fopen() call will fail by returning FALSEand generating an error of level E_WARNING. If the file does not exist,attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flagsfor the underlying open(2) system call. This option is supported in PHP 4.3.2and later, and only works for local files.

  • mode Description

    'x+'

    Create and open for reading and writing; place the file pointer at thebeginning of the file. If the file already exists, the fopen() call will fail byreturning FALSE and generating an error of level E_WARNING. If the filedoes not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option issupported in PHP 4.3.2 and later, and only works for local files.

    contoh :

    Fungsi fclose

    Fungsi fclose digunakan untuk menutup berkasBentuk pemanggilannya :

    fclose (pegangan) pegangan : pegangan berkas yang diperoleh saat memanggil fopen.Returns TRUE on success or FALSE on failure.

    contoh :

  • Fungsi fputs

    Fungsi fputs digunakan untuk merekam data ke berkas.Bentuk Pemanggilan :

    fputs(pegangan, data)

    Fungsi fgets

    Fungsi fgets digunakan untuk membaca data yang terdapat pada berkas.Bentuk pemanggilan :

    fgets(pegangan, panjang) pegangan : pegangan berkas yang diperoleh saat memanggil fopen. Panjang : jumlah karakter yang akan dibaca.Contoh :

    Fungsi feof

    Fungsi eof berguna untuk memk.erikasa apakah penunjuk berkas sedang menunjuk akhirberkas atau tidak.Bentuk pemanggilan :

    feof(pegangan)