PHP ဖိုင်ကို ဖွင့်/ဖတ်/ပိတ်ပါ။
ဤအခန်းတွင် ဆာဗာပေါ်တွင် ဖိုင်တစ်ဖိုင်ကို ဖွင့်ခြင်း၊ ဖတ်ခြင်းနှင့် ပိတ်နည်းကို သင်ပေးပါမည်။
PHP Open File - fopen()
ဖိုင်များကိုဖွင့်ရန် ပိုမိုကောင်းမွန်သောနည်းလမ်းမှာ fopen()
လုပ်ဆောင်ချက်ဖြင့်ဖြစ်သည်။ ဤလုပ်ဆောင်ချက်သည် သင့်အား လုပ်ဆောင်ချက်ထက် ရွေးချယ်စရာများ ပိုမိုပေးသည် readfile()
။
သင်ခန်းစာများအတွင်း ကျွန်ုပ်တို့သည် စာသားဖိုင်၊ "webdictionary.txt" ကို အသုံးပြုပါမည်-
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
ပထမဘောင် fopen()
တွင် ဖွင့်ရမည့်ဖိုင်အမည်ပါရှိပြီး ဒုတိယဘောင်သည် မည်သည့်မုဒ်တွင် ဖိုင်ကိုဖွင့်သင့်သည်ကို သတ်မှတ်သည်။ fopen() လုပ်ဆောင်ချက်သည် သတ်မှတ်ထားသောဖိုင်ကို မဖွင့်နိုင်ပါက အောက်ပါဥပမာသည် စာတိုတစ်စောင်ကိုလည်း ထုတ်ပေးသည်-
ဥပမာ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
အကြံပြုချက်- ၎င်း fread()
နှင့် fclose()
လုပ်ဆောင်ချက်များကို အောက်တွင် ရှင်းပြပါမည်။
ဖိုင်ကို အောက်ပါမုဒ်များထဲမှ တစ်ခုဖြင့် ဖွင့်နိုင်သည်-
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP Read File - fread()
လုပ်ဆောင်ချက်သည် အ fread()
ဖွင့်ဖိုင်တစ်ခုမှ ဖတ်သည်။
၏ပထမဘောင် fread()
တွင်ဖတ်ရန်ဖိုင်အမည်ပါရှိပြီးဒုတိယဘောင်သည်ဖတ်ရန်အများဆုံးဘိုက်အရေအတွက်ကိုသတ်မှတ်သည်။
အောက်ပါ PHP ကုဒ်သည် "webdictionary.txt" ဖိုင်ကို အဆုံးအထိ ဖတ်ပြသည်-
fread($myfile,filesize("webdictionary.txt"));
PHP Close File - fclose()
ဖွင့် ထားသော fclose()
ဖိုင်ကိုပိတ်ရန် function ကိုအသုံးပြုသည်။
၎င်းတို့နှင့် ပြီးပါက ဖိုင်များအားလုံးကို ပိတ်ရန် ပရိုဂရမ်ရေးဆွဲခြင်း အလေ့အကျင့်ကောင်းတစ်ခု ဖြစ်ပါသည်။ သင့်ဆာဗာပေါ်တွင် အရင်းအမြစ်များကို ထုတ်ယူနေသည့် ပွင့်လင်းဖိုင်ကို သင်မလိုချင်ပါ။
fclose()
ကျွန်ုပ်တို့ပိတ်လိုသော ဖိုင်အမည် (သို့မဟုတ် ဖိုင်အမည်ကို ကိုင်ဆောင်ထားသော ကိန်းရှင်တစ်ခု) လိုအပ်သည် -
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP တစ်ကြောင်းတည်းဖတ်ပါ - fgets()
fgets()
ဖိုင်တစ်ခုမှ စာကြောင်းတစ်ကြောင်းကို ဖတ်ရန် လုပ်ဆောင်ချက်ကို အသုံးပြုသည် ။
အောက်ပါဥပမာသည် "webdictionary.txt" ဖိုင်၏ ပထမစာကြောင်းကို ထုတ်ပေးသည်-
ဥပမာ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
မှတ်ချက်fgets()
။ _
PHP Check End-of-File - feof()
function သည် feof()
"end-of-file" (EOF) သို့ရောက်ရှိခြင်းရှိမရှိစစ်ဆေးသည်။
အဆိုပါ feof()
လုပ်ဆောင်ချက်သည် အမည်မသိ အရှည်ရှိသော အချက်အလက်များကို လှည့်ပတ်ခြင်းအတွက် အသုံးဝင်သည်။
အောက်ဖော်ပြပါ ဥပမာသည် "webdictionary.txt" ဖိုင်ကို ဖိုင်အဆုံးအထိ ရောက်သည်အထိ တစ်ကြောင်းပြီးတစ်ကြောင်း ဖတ်သည်-
ဥပမာ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP Read Single Character - fgetc()
fgetc()
ဖိုင်တစ်ခုမှ စာလုံးတစ်လုံးကို ဖတ်ရန် လုပ်ဆောင်ချက်ကို အသုံးပြုသည် ။
အောက်ဖော်ပြပါ ဥပမာသည် ဖိုင်၏အဆုံးသို့မရောက်မချင်း "webdictionary.txt" ဖိုင်ကို အက္ခရာစာလုံးအလိုက် ဖတ်ပြသည်-
ဥပမာ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
မှတ်ချက်fgetc()
။ _
PHP Filesystem Reference ကို ပြီးအောင်လုပ်ပါ။
ဖိုင်စနစ်လုပ်ဆောင်ချက်များကို အပြည့်အစုံကိုးကားရန်အတွက်၊ ကျွန်ုပ်တို့၏ ပြီးပြည့်စုံသော PHP Filesystem Reference သို့ သွားပါ ။