วันอังคารที่ 21 ธันวาคม พ.ศ. 2553

delete ไฟล์ ด้วยฟังก์ชัน unlink()

delete ไฟล์ ด้วยฟังก์ชัน unlink()

รูปแบบ
unlink(filename)
filename คือ ที่อยู่ของไฟล์

ตัวอย่าง
<?php

$file = "c:\\test.txt";

if (unlink($file))
{
echo ("deleted $file");
}
else
{
echo ("error");
}

?>
จากตัวอย่าง
ถ้า delete สำเร็จจะ return TRUE ทำให้พิมพ์ deleted c:\test.txt
ถ้าไม่สำเร็จจะ return FALSE ทำให้พิมพ์ error

สร้างไฟล์ชั่วคราว ด้วยฟังก์ชัน tmpfile()

สร้างไฟล์ชั่วคราว ด้วยฟังก์ชัน tmpfile()

รูปแบบ
tmpfile()
*ไม่มีพารามิเตอร์

ค่า return
resource ไฟล์

ตัวอย่าง
<?php
$temp = tmpfile();
fwrite($temp, "TEST");
fclose($temp);

?>

สร้างไฟล์ชั่วคราว ด้วยฟังก์ชัน tempnam()

สร้างไฟล์ชั่วคราว ด้วยฟังก์ชัน tempnam()

รูปแบบ
tempnam(dir,prefix)
dir คือ ที่อยู่ของไฟล์
prefix คือ คำนำหน้าชื่อของไฟล์

ค่า return
path ของไฟล์

ตัวอย่าง
<?php
echo tempnam("C:\\","tem");

?>
จากตัวอย่าง จะได้ผลลัพธ์ออกมาเป็น C:\temFB.tmp

ทดสอบว่าจบไฟล์หรือยัง ด้วยฟังก์ชัน feof()

ทดสอบว่าจบไฟล์หรือยัง ด้วยฟังก์ชัน feof()


ตัวอย่าง
<?php

$file = fopen("test.txt", "r") ;

while(!feof($file))
{
echo fgets($file). "<br />";
}

fclose($file);

?>

ปิดไฟล์ ด้วยฟังก์ชัน fclose()

ปิดไฟล์ ด้วยฟังก์ชัน fclose()


ตัวอย่าง
<?php

$file=fopen("c:\\welcome.txt","w+");
fclose($file);

?>

หาข้อมูลของไฟล์ ด้วยฟังก์ชัน fstat()

หาข้อมูลของไฟล์ ด้วยฟังก์ชัน fstat()

รูปแบบ
fstat(file)
file คือ resource ของไฟล์

ฟังก์ชันจะ return ค่าเป็น array โดยมี element ดังนี้
[0] หรือ [dev] คือ Device number
[1] หรือ [ino] คือ Inode number
[2] หรือ [mode] คือ Inode protection mode
[3] หรือ [nlink] คือ Number of links
[4] หรือ [uid] คือ User ID of owner
[5] หรือ [gid] คือ Group ID of owner
[6] หรือ [rdev] คือ Inode device type
[7] หรือ [size] คือ Size in bytes
[8] หรือ [atime] คือ Last access (รูปแบบ Unix timestamp)
[9] หรือ [mtime] คือ Last modified (รูปแบบ Unix timestamp)
[10] หรือ [ctime] คือ Last inode change (รูปแบบ Unix timestamp)
[11] หรือ [blksize] คือ Blocksize of filesystem IO (if supported)
[12] หรือ [blocks] คือ Number of blocks allocated

ตัวอย่าง
<?php
$file = fopen("test.txt","r");
print_r(fstat($file));
fclose($file);

?>

หาไฟล์หรือ directory ที่เข้ารูปแบบที่กำหนด ด้วยฟังก์ชัน glob()

หาไฟล์หรือ directory ที่เข้ารูปแบบที่กำหนด ด้วยฟังก์ชัน glob()

รูปแบบ
glob(pattern)
pattern คือ รูปแบบ


ตัวอย่าง
<?php
print_r(glob("*.txt"));

?>