2008/10/03

รวม function เกี่ยวกับการจัดการ file ของ PHP

is_dir() ตรวจสอบชื่อไฟล์นั้นเป็น ได้เรกรอรี่หรือไม่ is_dir(files-name)
is_executable() ตรวจสอบว่าไฟล์นั้น เอ็กซิคิวส์ได้หรือไม่ is_executable(files_name)
is_file() ตรวจสอบว่าไฟล์นั้นเป็นไฟล์ธรรมดาหรือไม่ is_file(files-name)
is_readable() ตรวจสอบว่าไฟล์นั้นอ่านได้หรือไม่ is_readable(files-name)
is_writeable() ตรวจสอบว่าไฟล์นั้นเขียนได้หรือไม่ is_writeable(files-name)
Readfile() อ่านข้อมุลจากไฟล์โดยไม่ต้องเปิดไฟล์ Readfile(files-name)
Rename() เปลี่ยนชื่อไฟล์ Rename(oldname,newname)
Rewind() เลือนตัวชี้ไปยังตัวแรกหรือต้นของ ไดเรคทอรี่ Rewind(file-handdle)
Stat() แสดงรายละเอียดของไฟล์ Stat(filename)
Touch() กำหนดเวลาในการปรับแต่งไหล์ Touch(files-name,[time])
Unlink() ลบไฟล์ที่ต้องการ Unlink(files-name)
Basename() แสดงชื่อไฟล์ไม่รวม path Basename(path)
chgrp() เปลี่ยนกลุ่มของไฟล์ Chgrp(files-name-group)
Chmod() เปลี่ยนโหมดของไฟล์ Chmod(files-name,mode)
Clearstatcache() Clear รายละเอียดต่างภายใน แคช Clearstatcache()
Copy() copy ไฟล์ Copy(ต้นทาง,ปลายทาง)
Dirname() แสดงชื่อ Directory ไม่รวม path Dirname(path)
Diskfreespace() หาเนื้อที่ว่างใน Directory Diskfreespace(directory)
Fclose() ปิดไฟล์ที่เปิดอยู่ Fclose(file_handle)
Fwrite() เขียนข้อความลงไฟล์ Fwrite(file_hand,text)
Ftell() บอกต่ำแหน่งพอยเตอร์ที่อยู่ในไฟล์ Ftell(file-handle)
Fseek() เลือกพอยเตอร์ไปข้างหน้า Fseek(file_handle,offset)
Fread() อ่านข้อมูลจากไฟล์ สามารถระบุจำนวนความยาวที่จะอ่านได้ Fread(file_handle,lenth)
Fputs() เขียนข้อความลงไฟล์ ถ้าไฟล์มีอยู่แล้ว จะลบไฟล์เก่าและเขียนทับ Fputs(files-name,text) Fopen() เปิดไฟล์ Fopen(files-name,mode)
Filetype() ตรวจสอบประเภทไฟล์ Filetype(files-name)
Filesize() ตรวจสอบขนาดไฟล์ Filesize(files-name)
Fileperms() ตรวจสอบการกำหนดสิทธ์ของไฟล์ Fileperms(files-name)
Fileowner() ตรวจสอบชื่อผู้สร้างไฟล์ Fileowner(files-name)
Filemtime() ตรวจสอบว่า update ครั้งล่าสุดเมื่อใด Filemtime(files-name)
Filegroup() ตรวจสอบกลุ่มของไฟล์ Filegroup(files-name)
Filectime() ตรวจสอบไฟล์ถูกสร้างเมื่อไหร่ Filectime(files-name)
Fileatime() ตรวจสอบไฟล์ถูหใช้งานครั้งสุดท้ายเมื่อไหร่ Fileatime(files-name)
File_exists() ตรวจสอบไฟล์นั้นมีอยู่จริงหรือไม่ File_exists(files-name)
File() อ่านไฟล์แต่ละบรรทัดลงใน Array File(files-name)
Fgetss() อ่านข้อมูลแต่ละบรรทัดหรือแต่ละอักษร โดยตัด แท็ก Html ออก Fgetss(file_handle,length) Fgets() อ่านข้อมูลแต่ละบรรทัดหรือแต่ละอักษร Fgets(file_handle,length) Fgetc() อ่านข้อมูลที่ละอักษร Fgetc(file_handle)
Feof() ตรวจสอบว่า พอยเตอร์เลื่อนไปอยู่ท้ายไฟล์หรือไม่ Feof(file_handle)


มาดูตัวอย่างกันหน่อยครับ

Sample1.php เป็นการแสดงรายละเอียดต่าง ๆ ของไฟล์ index.html อยู่ใน

DirectoryC:\Appserv\www\index.html

echo"Dirname คือ ".Dirname($files);
echo"Basename คือ ".Basename($files);
echo"Filetype คือ ".Filetype($files);
echo"Filesize คือ ".Filesize($files);
echo"Fileperms คือ ".Fileperms($files);
echo"Fileowner คือ ".Fileowner($files);
echo"Filemtime คือ ".Filemtime($files);
echo"Filegroup คือ ".Filegroup($files);
echo"Filectime คือ ".Filectime($files);
echo"Fileatime คือ ".Fileatime($files);
echo"is_dir คือ ".is_dir($files);
echo"is_file คือ ".is_file($files);
echo"is_readable คือ ".is_readable($files);
echo"is_writeableคือ ".is_writeable($files);

?>

Out Put

File
ตัวอย่างการสร้างไฟล์


ตัวอย่างต่อไปนี้เป็นการเปิดไฟล์ขึ้นมาจากนั้นให้เขียนต่อข้อมูลที่มีอยู่แล้ว


$FILE =fopen("thaicreate.txt","a");
fputs( $FILE, "ในส่วนนี้เป็นการเขียนต่อจากข้อมูลเดิม\n");
fclose($FILE);
?>

ลองมาดูอีกตัวอย่างครับ
is_php@hotmail.com";
$desc.= " ชื่อ : $nome ";$desc.= " ที่อยู่ : $address";$desc.= " อีเมล์ : $email ";
$filename = "C:\ThaiCreate.txt";
if (!$filename = fopen($filename, "w")) {

echo "ไม่สามารถเขียนสร้างไฟล์ ($filename)";
exit;
}
if (!fwrite($filename, $desc))
{
print "ไม่สามารถเขียนข้อมูลลงไฟล์ ($filename)";
exit;
}
echo "เขียนข้อมูลลงไฟล์สำเร็จ";
fclose($filename);

?>


ตัวอย่างนี้เป็นการอ่านข้อมูลที่มีอยู่ใน text มาแสดง

$FILE =fopen("thaicreate.txt","r");
while (!feof($FILE)) {
$text = fgets($FILE, 4096);
echo $text;
}
fclose($FILE);
?>

No comments: