file & directory management การจัดการไฟล์และ...
DESCRIPTION
File & Directory Management การจัดการไฟล์และ ไดเรคทอ รี. Content. 1. Open/Write/Close File Functions 2. Read Data Functions 3. Other File Operations 4. Directory Functions 5. Other Directory Operations. Open/Write/Close a File. fopen () เพื่อใช้ในการเปิดไฟล์ . - PowerPoint PPT PresentationTRANSCRIPT
File & Directory ManagementFile & Directory Managementการจั�ดการไฟล์แล์ะไดเรคทอร�การจั�ดการไฟล์แล์ะไดเรคทอร�
ContentContent
1. Open/Write/Close File Functions
2. Read Data Functions
3. Other File Operations
4. Directory Functions
5. Other Directory Operations
Open/Write/Close a File fopen() เพื่��อใช้�ในการเปิ�ดไฟล์�.
fwrite() เพื่��อใช้�ในการเขี�ยนไฟล์�
fclose() เพื่��อใช้�ในการปิ�ดไฟล์�
mode ที่��ใช้�ในการเปิ�ดไฟล์�.
r อ�านอย�างเด�ยว w สร�างไฟล์�โดยถ้�ามี�ไฟล์�เด�มีอย �แล์�วจะที่$าการล์บที่�&งแล์ะ
สร�างไฟล์�ขี'&นมีาใหมี� a กรณี�ที่��มี�ไฟล์�อย �แล์�วจะที่$าการเขี�ยนไฟล์�ต่�อจากที่��มี�อย �
File Open/Close SyntaxSyntax
fopen(filename, mode);
fwrite(filename,text);
fclose(object);
File Open/Close Example<?php $toread = fopen('text.txt','r');
while (!feof($toread)) {echo fgets($toread, 1024);echo '<br />';
}
fclose($toread);?>
File Write Example<?php
$towrite = fopen('text.txt','w');
fwrite($towrite,"PHP Write File Line1\r\n");fwrite($towrite,"PHP Write File Line2\r\n");
fclose($towrite);
?>
File Write Example<?php
$towrite = fopen('text.txt',‘a');
fwrite($towrite,"PHP Write File Line1\r\n");fwrite($towrite,"PHP Write File Line2\r\n");
fclose($towrite);
?>
Read DataThere are two main functions to read data:
fgets($handle,$bytes) Reads up to $bytes of data, stops at newline or end of file (EOF)
fread($handle,$bytes) Reads up to $bytes of data, stops at EOF.
Read Data
We need to be aware of the End Of File (EOF) point..
feof($handle) Whether the file has reached the EOF point. Returns true if have reached EOF.
Read Data
We need to be aware of the End Of File (EOF) point..
feof($handle) Whether the file has reached the EOF point. Returns true if have reached EOF.
File Open/Close Example<?php $toread = fopen('text.txt','r');
while (!feof($toread)) {echo fgets($toread, 1024);echo '<br />';
}
fclose($toread);?>
Other File Operations Delete file
unlink('filename');
Rename (file or directory)rename('old name', 'new name');
Copy filecopy('source', 'destination');
And many, many more! www.php.net/manual/en/ref.filesystem.php
Directories Open a directory
$handle = opendir('dirname');$handle 'points' to the directory
Read contents of directoryreaddir($handle)Returns name of next file in directoryFiles are sorted as on file system
Close a directoryclosedir($handle)Closes directory 'stream'
Directory Example<?php
$handle = opendir('./');
while(false !== ($file=readdir($handle))){
echo "$file <br />";}
closedir($handle);?>
Other Directory Operations Get current directory
getcwd()
Change Directorychdir('dirname');
Create directorymkdir('dirname');
Delete directory (MUST be empty)rmdir('dirname');
And more!www.php.net/manual/en/ref.dir.php
แบบฝึ�กหั�ดจงเขี�ยนโปิรแกรมีสร�าง Directory แล์ะ บ,นที่'กไฟล์� .txt รายล์ะเอ�ยดมี�ด,งน�&
1 )รห,ส ช้��อ นามีสก-ล์2) ที่��อย �3) ที่��ที่$างาน
Q & A