Thursday, 27 March 2014


Bab 16


PHP File Handling


 

Dalam PHP, fungsi fopen() digunakan untuk membuka file.

 

Membuka File


 


Program16-1.php

<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>

 

Mode pembukaan file

Mode
Keterangan
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 clears the contents of file; or creates a new file if it doesn't exist
w+
Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a
Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+
Read/Append. 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

Catatan: Jika fopen() tidak dapat membuka file, maka akan mengembalikan nilai 0 (false).

 

Program16-2.php

<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");

?>
</body>
</html>

 

 

Menutup File


Program16-3.php

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

 

 

Memeriksa EOF (End Of File)


 


Catatan: Kita tidak dapat membaca file yang terbuka dalam mode w, a, dan x!

if (feof($file)) echo "End of file";

 

Membaca file baris per baris (fgets())


 


Program16-4.php

<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>

 

 

Membaca file karakter per karakter (fgetc())


 


Program16-5.php


<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file)) 
  { 
  echo fgetc($file); 
  }
fclose($file);
?>

 

 



Bab 16


PHP File Handling


 

Dalam PHP, fungsi fopen() digunakan untuk membuka file.

 

Membuka File


 


Program16-1.php

<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>

 

Mode pembukaan file

Mode
Keterangan
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 clears the contents of file; or creates a new file if it doesn't exist
w+
Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a
Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+
Read/Append. 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

Catatan: Jika fopen() tidak dapat membuka file, maka akan mengembalikan nilai 0 (false).

 

Program16-2.php

<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");

?>
</body>
</html>

 

 

Menutup File


Program16-3.php

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

 

 

Memeriksa EOF (End Of File)


 


Catatan: Kita tidak dapat membaca file yang terbuka dalam mode w, a, dan x!

if (feof($file)) echo "End of file";

 

Membaca file baris per baris (fgets())


 


Program16-4.php

<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>

 

 

Membaca file karakter per karakter (fgetc())


 


Program16-5.php


<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file)) 
  { 
  echo fgetc($file); 
  }
fclose($file);
?>

 

 



Bab 15


PHP Include File


 

 

Server Side Includes (SSI) digunakan untuk menyimpan fungsi, header, footer, atau elemen-elemen yang dapat digunakan pada halaman yang berlainan.

 

Server Side Includes


 

Fungsi include()


Fungsi include() akan mengambil semua teks pada file include dan mengkopinya ke file tujuan.


 


Program15-1.php


Diasumsikan bahwa kita mempunyai file header dengan nama “header.php”. Untuk memakai file ini pada halaman web kita seperti di bawah ini.

<html>
<body>
<?php include("header.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

 


Program15-2.php


Sekarang, kita asumsikan bahwa kita mempunyai file standar menu yang akan digunakan pada seluruh halaman (file include biasanya berektensi *.php). Penggunaannya seperti di bawah ini.

<html>
<body>
<a href="http://www.w3schools.com/default.php">Home</a> |
<a href="http://www.w3schools.com/about.php">About Us</a> | 
<a href="http://www.w3schools.com/contact.php">Contact Us</a>

 

Ketiga file, "default.php", "about.php", dan "contact.php" semuanya akan di-include-kan pada file "menu.php". Berikut ini program "default.php":

<?php include("menu.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

 

Dan hasilnya pada browser adalah sebagai berikut.

<html>
<body>
<a href="default.php">Home</a> |
<a href="about.php">About Us</a> | 
<a href="contact.php">Contact Us</a>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

 

Fungsi require()


Fungsi require() sama dengan include(), tetapi berbeda dalam cara penanganan kesalahan.

Fungsi include() akan menghasilkan peringatan (dan program akan melanjutkan ekseskusinya) sedangkan fungsi require() akan menghasilkan fatal error dan menghentikan program.

Program15-3.php (program contoh error pada penggunaan fungsi include()).

<html>
<body>
 
<?php
include("wrongFile.php");
echo "Hello World!";
?>
 
</body>
</html>

 

Error message:

Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5
Hello World!

 

Program15-4.php (program contoh error pada penggunaan fungsi require())

<html>
<body>
 
<?php
require("wrongFile.php");
echo "Hello World!";
?>
 
</body>
</html>

 

Error message:

Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Fatal error: require() [function.require]:
Failed opening required 'wrongFile.php'
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5


Bab 15


PHP Include File


 

 

Server Side Includes (SSI) digunakan untuk menyimpan fungsi, header, footer, atau elemen-elemen yang dapat digunakan pada halaman yang berlainan.

 

Server Side Includes


 

Fungsi include()


Fungsi include() akan mengambil semua teks pada file include dan mengkopinya ke file tujuan.


 


Program15-1.php


Diasumsikan bahwa kita mempunyai file header dengan nama “header.php”. Untuk memakai file ini pada halaman web kita seperti di bawah ini.

<html>
<body>
<?php include("header.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

 


Program15-2.php


Sekarang, kita asumsikan bahwa kita mempunyai file standar menu yang akan digunakan pada seluruh halaman (file include biasanya berektensi *.php). Penggunaannya seperti di bawah ini.

<html>
<body>
<a href="http://www.w3schools.com/default.php">Home</a> |
<a href="http://www.w3schools.com/about.php">About Us</a> | 
<a href="http://www.w3schools.com/contact.php">Contact Us</a>

 

Ketiga file, "default.php", "about.php", dan "contact.php" semuanya akan di-include-kan pada file "menu.php". Berikut ini program "default.php":

<?php include("menu.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

 

Dan hasilnya pada browser adalah sebagai berikut.

<html>
<body>
<a href="default.php">Home</a> |
<a href="about.php">About Us</a> | 
<a href="contact.php">Contact Us</a>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

 

Fungsi require()


Fungsi require() sama dengan include(), tetapi berbeda dalam cara penanganan kesalahan.

Fungsi include() akan menghasilkan peringatan (dan program akan melanjutkan ekseskusinya) sedangkan fungsi require() akan menghasilkan fatal error dan menghentikan program.

Program15-3.php (program contoh error pada penggunaan fungsi include()).

<html>
<body>
 
<?php
include("wrongFile.php");
echo "Hello World!";
?>
 
</body>
</html>

 

Error message:

Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5
Hello World!

 

Program15-4.php (program contoh error pada penggunaan fungsi require())

<html>
<body>
 
<?php
require("wrongFile.php");
echo "Hello World!";
?>
 
</body>
</html>

 

Error message:

Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Fatal error: require() [function.require]:
Failed opening required 'wrongFile.php'
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5

Blogger news

BTemplates.com

Powered by Blogger.

You can replace this text by going to "Layout" and then "Page Elements" section. Edit " About "

Search This Blog

Pointer

Blogger Tricks

Popular Posts