Operasi Dasar File.txt dengan PHP
Publish Date : 11-02-2016 , dibaca 9622 kali, Hari ini dibaca : 1 kali ,0 comments
Kita dapat Memanipulasi File .txt , terkadang seorang programer membutuhkan file .txt dalam mengembangkan project yang dibuatnya... Kali ini suckittrees dot com akan berbagi script / operasi dasar dalam mengelola file.txt
Berikut Operasi Dasar file.txt dengan PHP
1. CreateFile .txt
<?php
$file = 'file.txt';
//To Create File
$fp = fopen($file, 'w') or die('Cannot open file: '.$file);
if($fp){
echo "file created successfully";
}
fclose($fp);
?>
2. Write File txt
<?php
//To Write to the file
$file = 'file.txt';
$fp = fopen($file, 'w') or die('Cannot open file: '.$file);
$data = 'Line1:This is Line1 data';
fwrite($fp, $data);
fclose($fp);
?>
3. Read File txt
<?php
//To Read File
$file = 'file.txt';
$fp = fopen($file, 'r');
$data = fread($fp,filesize($file));
echo $data;
?>
4. Append File txt
<?php
//append to the file
$file = 'file.txt';
//opening a file in append mode.
$fp = fopen($file, 'a') or die('Cannot open file: '.$file);
$data = 'Line no.1: Data on first line'."n";
fwrite($fp, $data);
fclose($fp);
?>
5. Delete File txt
<?php
//deleteing file from hard desk
$file = 'file.txt';
unlink($file);
?>