Hallo sobat blogger baru update tutorial lagi neh..hehehehe maaf yah ada urusan sama otak
hehehe.....
Ok,kali ini saya masih membahas tutorial PHP dan kali ini saya menjelaskan tentang FUNGSI FILE
SYSTEM
pada
PHP..
8. FUNGSI FILESYSTEM
1. basename Menghasilkan nama file dari suatu path
2. chmod Mengubah mode akses file
3. chown Mengubah pemilik file
4. copy Meng-copy file
5. dirname Menghasilkan nama direktori dari suatu path file
6. disk_free_space Menghasilkan sisa space disk suatu direktori
7. disk_total_space Menghasilkan total size direktori
8. fclose Menutup file yang dibuka
9. fgetc Mengambil karakter-karakter suatu file teks
10. fgets Mengambil baris-baris dari file
11. file_exists Cek apakah file atau direktori tersebut ada atau tidak
12. file_get_contents Membaca isi file keseluruhan
13. file_put_contents Menulis teks ke file
14. file Membaca isi file ke dalam array
15. fileatime Mendapatkan waktu akses terakhir suatu file
16. filesize Mendapatkan ukuran file
17. filetype Mendapatkan tipe file
18. fopen Membuka file atau URL
basename
Fungsi
:
Menghasilkan
nama
file
dari
suatu
path
Sintaks :
basename ( string $path [, string $suffix ] )
Parameter
:
$path, adalah alamat file dengan lokasi direktorinya, contoh : /var/www/gambar/fileku.jpg
$suffix,
akan
menghilangkan
akhirannya
sesuai
string
$suffix
Contoh :
<?php
$path = "/home/httpd/html/index.php";
$file = basename($path);
echo $file;
//Hasilnya index.php
$file = basename($path, ".php");
echo $file;
?>
//Hasilnya index
chmod
Fungsi
Sintaks :
Mengubah
mode
Contoh :
akses
file,
untuk
linux,
unix
<?php
// Read dan write hanya untuk pemilik
chmod("/direktori/filesaya", 0600);
// Pemilik bisa apa aja, read dan execute untuk others
chmod("/direktori/filesaya", 0755);
?>
chown
Fungsi
Sintaks :
Mengubah
pemilik
file
Contoh :
<?php
$file_name= "foo.php";
$path = "/home/sites/php.net/public_html/sandbox" . $file_name ;
$user_name = "root";
// Set pemilik file
chown($path, $user_name);
?>
copy
Fungsi
Sintaks :
Mengcopy
file
Parameter
$source
$dest,
Contoh :
adalah
adalah
file
<?php
$file = 'upload.php';
$newfile = 'uploadcopy.php';
if (!copy($file, $newfile)) {
echo "gagal copy file $file...\n";
}else{
yang
file
akan
:
dicopy
tujuan
dirname
Fungsi
:
Menghasilkan
nama
Sintaks :
dirname ( string $path )
Contoh :
<?php
$path = "/www/gambar/pic.jpg";
$file = dirname($path);
echo $file;
direktori
dari
suatu
path
file
//Hasilnya /www/gambar
?>
disk_free_space
Fungsi
Sintaks :
Menghasilkan
sisa
space
disk
suatu
direktori
(dalam
bytes)
Contoh :
<?php
//linux, unix
$df = disk_free_space("/");
//Windows:
echo disk_free_space("C:");
echo disk_free_space("D:");
?>
disk_total_space
Fungsi
Sintaks :
Menghasilkan
total
Contoh :
<?php
size
direktori
(dalam
bytes)
//linux , unix
$df = disk_total_space("/");
//Windows:
echo disk_total_space("C:")."<br>";
echo disk_total_space("D:");
?>
fclose
Fungsi
Sintaks :
Menutup
file
yang
dibuka
Contoh :
<?php
$file = fopen('somefile.txt', 'r');
fclose($file);
?>
fgetc
Fungsi
Sintaks :
Mengambil
karakter-karakter
suatu
file
teks
Contoh
Misal anda punya filesaya.txt, yang isinya :
Desrizal
<?php
$fp = fopen('filesaya.txt', 'r');
if (!$fp) {
echo 'filesaya.txt nggak ada';
}
while (false !== ($char = fgetc($fp))) {
echo "$char<br>";
}
?>
Hasilnya :
d
e
s
r
i
z
a
l
fgets
Fungsi
Sintaks :
Mengambil
baris-baris
dari
file
diambil
melebihi
:
barisnya
panjang file
Parameter
$handle,
adalah
file
$length, adalah panjang/ukuran file,
Contoh :
yang
isi aja
kira
akan
kira yang
<?php
$handle = @fopen("filesaya.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 1000000);
echo $buffer."<br>";
}
fclose($handle);
}
?>
file_exists
Fungsi
Sintaks :
Cek
apakah
Contoh :
<?php
$filename = 'filesaya.txt';
if (file_exists($filename)) {
echo "File $filename ada";
file
atau
direktori
tersebut
ada
} else {
echo "File $filename nggak ada";
}
?>
file_get_contents
Fungsi
Sintaks :
Membaca
isi
file/
web
keseluruhan
menjadi
string
Parameter
$filename,
Contoh :
file
atau
web
yang
akan
dibaca
:
isinya
<?php
$homepage = file_get_contents('http://blog.codingwear.com/');
echo $homepage;
?>
file_put_contents
Fungsi
Sintaks :
file_put_contents ( string
resource $context ]] )
$filename,
$data,
data
$flags,
option,
Contoh :
file
yang
kan
FILE_APPEND
Menulis
$filename
teks
mixed
$data
ke
[,
int
$flags=
yang
akan
ditulis
ke
file,
bisa
string
(jika
file
sudah
ada,
teks
akan
<?php
$file = 'data.txt';
$nama = 'Desrizal';
file_put_contents($file, $nama);
?>
Contoh di atas akan membuat file data.txt dan menuliskan Desrizal di data.txt
file
file
[,
ditulis
atau
array
ditambahkan)
Fungsi
Sintaks :
Membaca
isi
file
perbatis,
dimasukkan
ke
dalam
array
Parameter
$filenama,
file
yang
akan
dibaca
$flags, option :
FILE_IGNORE_NEW_LINES : Jangan tambahkan baris baru di akhir elemen array
FILE_SKIP_EMPTY_LINES : Abaikan baris yang kosong
:
isinya
Contoh :
<?php
$lines = file('filesaya.txt');
print_r($lines);
?>
fileatime
Fungsi
Sintaks :
Mendapatkan
waktu
akses
terakhir
suatu
file
Contoh :
<?php
$filename = 'filesaya.txt';
if (file_exists($filename)) {
echo "$filename terakhir kali diakses pada: " . date("F d Y H:i:s.",
fileatime($filename));
}
//filesaya.txt terakhir kali diakses pada: October 30 2010 17:34:13
?>
filesize
Fungsi
Sintaks :
Contoh :
<?php
$filename = 'filesaya.txt';
Mendapatkan
ukuran
file
filetype
Fungsi
Sintaks :
Mendapatkan
tipe
file
Contoh :
<?php
echo filetype('filesaya.txt');
echo filetype('/var/');
?>
// file
// dir
fopen
Fungsi
Sintaks :
Membuka
file
atau
URL
$filename,
$mode :
namafile
atau
URL
Description
'r' Dibuka hanya untuk untuk dibaca; pointer diletakkan di awal file
'r+' Dibuka untuk dobaca dan ditulis; pointer diletakkan di awal file
Dibuka hanya untuk ditulis; pointer diletakkan diawal file, jika file tidak ada, akan dibuat
'w'
otomatis
Dibuka untuk dibaca dan ditulis; pointer diletakkan diawal file, jika file tidak ada, akan
'w+'
dibuat otomatis
Dibuka hanya untukditulis; pointer diletakkan di akhir file, jika file belum ada, akan dibuat
'a'
otomatis.
Dibuka hanya untukdibaca dan ditulis; pointer diletakkan di akhir file, jika file belum ada,
'a+'
akan dibuat otomatis.
mode
Contoh :
<?php
$handle = fopen("/home/rasmus/file.txt", "r+");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password@example.com/somefile.txt", "w");
?>
is_dir
Fungsi:
Sintaks :
Cek
apakah
filename
adalah
sebuah
directori
Contoh :
<?php
if(is_dir('filesaya.txt')){
echo "Ini adalah direktori";
}else{
echo "Ini adalah file";
}
?>
is_executable
Fungsi:
Sintaks :
Cek
apakah
filename
bisa
di
eksekusi
Contoh :
<?php
$file = 'filesaya.txt';
if (is_executable($file)) {
echo $file.' bisa dieksekusi';
} else {
echo $file.' tidak bisa dieksekusi';
}
?>
is_file
Fungsi:
Sitaks
Cek
apakah
Contoh:
<?php
filename
adalah
sebuah
file
:
if(is_file('filesaya.txt')){
echo "Ini adalah file";
}else{
echo "Ini bukan file";
}
?>
is_readable
Fungsi:
Sintaks:
Cek
apakah
filename
bisa
dibaca
Contoh :
<?php
if(is_readable('filesaya.txt')){
echo "file ini bisa dibaca";
}else{
echo "file ini tidak bisa dibaca";
}
?>
is_writable
Fungsi:
Sintaks :
Cek
apakah
filename
bisa
ditulis
Contoh :
<?php
if(is_writable('filesaya.txt')){
echo "file ini bisa ditulis";
}else{
echo "file ini tidak bisa ditulis";
}
?>
mkdir
Fungsi:
Sintaks:
Membuat
direktori
Contoh:
<?php
mkdir("gambar", 0700);
?>
move_uploaded_file
Fungsi:
Sintaks:
Memindahkan
file
yang
diupload
ke
lokasi
baru
Contoh
Upload multiple file
<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
?>
rename
Fungsi:
Sintaks:
Rename
file
atau
directori
Contoh :
<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>
rmdir
Fungsi:
Sintaks:
Delete
direktori
Contoh:
<?php
rmdir('examples');
?>
unlink
Fungsi:
Sintaks:
Delete
Contoh:
<?php
unlink('test.html');
?>
file