Fungsi Filesystem

Diposting oleh Rini Pujiati on Sabtu, 14 April 2012

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 : Mengubah mode akses file, untuk linux, unix

Sintaks :

chmod ( string $filename , int $mode )

Contoh :

<?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 : Mengubah pemilik file

Sintaks :

chown ( string $filename , mixed $user )

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 : Mengcopy file

Sintaks :

copy ( string $source , string $dest [, resource $context ] )

Parameter :

$source , adalah file yang akan dicopy

$dest, adalah file tujuan

Contoh :

<?php
$file = 'upload.php';
$newfile = 'uploadcopy.php';

if (!copy($file, $newfile)) {
echo "gagal copy file $file...\n";
}else{
echo "File $file berhasil di copy";
}
?>

dirname

Fungsi : Menghasilkan nama direktori dari suatu path file

Sintaks :

dirname ( string $path )
Contoh :
<?php
$path = "/www/gambar/pic.jpg";
$file = dirname($path);
echo $file;

//Hasilnya /www/gambar
?>

disk_free_space

Fungsi : Menghasilkan sisa space disk suatu direktori (dalam bytes)

Sintaks :

disk_free_space ( string $directory )

Contoh :

<?php
//linux, unix
$df = disk_free_space("/");

//Windows:
echo disk_free_space("C:");
echo disk_free_space("D:");
?>

disk_total_space

Fungsi : Menghasilkan total size direktori (dalam bytes)

Sintaks :

disk_total_space ( string $directory )

Contoh :

<?php
//linux , unix
$df = disk_total_space("/");

//Windows:
echo disk_total_space("C:")."
"
;
echo disk_total_space("D:");
?>

fclose

Fungsi : Menutup file yang dibuka

Sintaks :

fclose ( resource $handle )

Contoh :

<?php
$file = fopen('somefile.txt', 'r');

fclose($file);
?>

fgetc

Fungsi : Mengambil karakter-karakter suatu file teks

Sintaks :

fgetc ( resource $handle )

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
"
;
}
?>

Hasilnya :

d
e
s
r
i
z
a
l

fgets

Fungsi : Mengambil baris-baris dari file

Sintaks :

fgets ( resource $handle [, int $length ] )

Parameter :

$handle, adalah file yang akan diambil barisnya

$length, adalah panjang/ukuran file, isi aja kira kira yang melebihi panjang file

Contoh :

<?php
$handle = @fopen("filesaya.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 1000000);
echo $buffer."
"
;
}
fclose($handle);
}
?>

file_exists

Fungsi : Cek apakah file atau direktori tersebut ada

Sintaks :

file_exists ( string $filename )

Contoh :

<?php
$filename = 'filesaya.txt';

if (file_exists($filename)) {
echo "File $filename ada";
} else {
echo "File $filename nggak ada";
}
?>

file_get_contents

Fungsi : Membaca isi file/ web keseluruhan menjadi string

Sintaks :

file_get_contents ( string $filename [, int $flags= 0 [, resource $context [, int $offset= -1
[, int $maxlen= -1 ]]]] )

Parameter :

$filename, file atau web yang akan dibaca isinya

Contoh :

<?php
$homepage = file_get_contents('http://blog.codingwear.com/');
echo $homepage;
?>

file_put_contents

Fungsi : Menulis teks ke file

Sintaks :

file_put_contents ( string $filename , mixed $data [, int $flags= 0 [, resource $context ]] )

$filename, file yang akan ditulis

$data, data yang kan ditulis ke file, bisa string atau array

$flags, option, FILE_APPEND (jika file sudah ada, teks akan ditambahkan)

Contoh :

<?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

Fungsi : Membaca isi file perbatis, dimasukkan ke dalam array

Sintaks :

file ( string $filename [, int $flags= 0 [, resource $context ]] )

Parameter :

$filenama, file yang akan dibaca isinya

$flags, option :

  • FILE_IGNORE_NEW_LINES : Jangan tambahkan baris baru di akhir elemen array
  • FILE_SKIP_EMPTY_LINES : Abaikan baris yang kosong

Contoh :

<?php
$lines = file('filesaya.txt');
print_r($lines);
?>

fileatime

Fungsi : Mendapatkan waktu akses terakhir suatu file

Sintaks :

fileatime ( string $filename )

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 : Mendapatkan ukuran file

Sintaks :

filesize ( string $filename )

Contoh :

<?php
$filename = 'filesaya.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>

filetype

Fungsi : Mendapatkan tipe file

Sintaks :

filetype ( string $filename )

Contoh :

<?php
echo filetype('filesaya.txt'); // file
echo filetype('/var/'); // dir
?>

fopen

Fungsi : Membuka file atau URL

Sintaks :

fopen ( string $filename , string $mode [, bool $use_include_path= false [, resource $context ]] )

$filename, namafile atau URL

$mode :

mode Description
'r' Dibuka hanya untuk untuk dibaca; pointer diletakkan di awal file
'r+' Dibuka untuk dobaca dan ditulis; pointer diletakkan di awal file
'w' Dibuka hanya untuk ditulis; pointer diletakkan diawal file, jika file tidak ada, akan dibuat otomatis
'w+' Dibuka untuk dibaca dan ditulis; pointer diletakkan diawal file, jika file tidak ada, akan dibuat otomatis
'a' Dibuka hanya untukditulis; pointer diletakkan di akhir file, jika file belum ada, akan dibuat otomatis.
'a+' Dibuka hanya untukdibaca dan ditulis; pointer diletakkan di akhir file, jika file belum ada, akan dibuat otomatis.

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: Cek apakah filename adalah sebuah directori

Sintaks :

is_dir ( string $filename )

Contoh :

<?php
if(is_dir('filesaya.txt')){
echo "Ini adalah direktori";
}else{
echo "Ini adalah file";
}
?>

is_executable

Fungsi: Cek apakah filename bisa di eksekusi

Sintaks :

is_executable ( string $filename )

Contoh :

<?php
$file = 'filesaya.txt';

if (is_executable($file)) {
echo $file.' bisa dieksekusi';
} else {
echo $file.' tidak bisa dieksekusi';
}
?>

is_file

Fungsi: Cek apakah filename adalah sebuah file

Sitaks :

is_file ( string $filename )

Contoh:

<?php
if(is_file('filesaya.txt')){
echo "Ini adalah file";
}else{
echo "Ini bukan file";
}
?>

is_readable

Fungsi: Cek apakah filename bisa dibaca

Sintaks:

is_readable ( string $filename )

Contoh :

<?php
if(is_readable('filesaya.txt')){
echo "file ini bisa dibaca";
}else{
echo "file ini tidak bisa dibaca";
}
?>

is_writable

Fungsi: Cek apakah filename bisa ditulis

Sintaks :

is_writable ( string $filename )

Contoh :

<?php
if(is_writable('filesaya.txt')){
echo "file ini bisa ditulis";
}else{
echo "file ini tidak bisa ditulis";
}
?>

mkdir

Fungsi: Membuat direktori

Sintaks:

mkdir ( string $pathname [, int $mode= 0777 [, bool $recursive= false [, resource $context ]]] )

Contoh:

<?php
mkdir("gambar", 0700);
?>

move_uploaded_file

Fungsi: Memindahkan file yang diupload ke lokasi baru

Sintaks:

move_uploaded_file ( string $filename , string $destination )

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: Rename file atau directori

Sintaks:

rename ( string $oldname , string $newname [, resource $context ] )

Contoh :

<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>

rmdir

Fungsi: Delete direktori

Sintaks:

rmdir ( string $dirname [, resource $context ] )

Contoh:

<?php
rmdir('examples');
?>

unlink

Fungsi: Delete file

Sintaks:

unlink ( string $filename [, resource $context ] )

Contoh:

<?php
unlink('test.html');
?>


sumber: codingware

{ 0 komentar... read them below or add one }

Posting Komentar