PHP ?? ??
fopen() ??? PHP?? ??? ?? ? ?????.
?? ??
PHP?? ??? ?? ?? ??? ????. ??? ???? ??? fopen() ??? ?????. fopen() ??? ???? ??? ? ? ??? ???? ??? ?? ??? ???? ???? ?????. ??? ???? ??? FALSE? ?????.
??
resource fopen(string $filename, string mode)
? ??? ? ?? ?????? ??? ??? ??? ?????. , ? ?? ????? ??? ?? ? ??? ??? ?????.
<html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html>
??? ?? ??? ? ? ????.
Mode ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?> r ?? ?????. ??? ?? ???? ?????. <<>
R+ ??/??. ??? ?? ???? ?????. ~ ~???????????????????????????????????????????????????. ?? ??? ?? ????. ??? ??? ? ??? ????. ~ ~ ?? ??? ?? ????. ??? ??? ? ??? ????. ~??????????????????????????????. ??? ?? ?? ?? ???, ??? ??? ? ??? ????. ~????????????????????????????????????????????????????????? ?? ???? ?? ??? ?????. ??????????????????????????????????? ??? ????. ??? ?? ???? ?? FALSE? ??? ?????. ~???????????????????????????????????. ? ??? ????. ??? ?? ???? ?? FALSE? ??? ?????.
??: fopen() ??? ??? ??? ? ? ??? 0(false)? ?????. ?
<html> <body> <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); // 不能打開指定文件的錯(cuò)誤信息: Unable to open file ?> </body> </html>Close the filefclose () ?? ??? ?? ? ???? ??:
<?php $file = fopen("test.txt","r"); //執(zhí)行一些代碼 fclose($file); ?>
?? ?(EOF) ??
feof() ??? ?? ?(EOF) ??? ?????. )? ??????.
feof() ??? ??? ? ? ?? ???? ??? ? ?????.
??: w, a ? x ????? ?? ?? ??? ?? ? ????!
if (feof($file)) echo "?? ?";??? ? ?? ??
fgets() ??? ???? ? ?? ??? ?? ? ?????.
string fgets( int handler [, int length] )
fgets()? ??? ???? ???? ? ?? ?? ?? ??-1??? ??? ???? ?????. ?? ??(?? ?? ???), EOF ?? length-1???? ?? ?? ?????. ??? ???? ??? ???? 1K ?? 1024??????.
?
?? ???? ??? ?? ??? ??? ? ?? ????.
<?php $file = fopen("welcome.txt", "r") or exit("無法打開文件!"); // 讀取文件每一行,直到文件結(jié)尾 while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file); ?>
??? ???? ????
fgetc() ??? ???? ???? ??? ?? ? ?????.
??: ? ??? ???? ?? ???? ?? ??? ?????.
?
?? ?? ??? ?? ??? ??? ? ??? ????.
<?php $file=fopen("welcome.txt","r") or exit("無法打開文件!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?>
?? ??? ????
fread() ??? ??? ?? ? ?????(???? ??? ???).
??:
string fread( int handler, int length )
fread()? ?? ??? ???? ?? length ???? ????. ?? ?? ? ??? ???? ?? ??? ?????.
?? ?? ???? ?? ?
?? ?(EOF)? ??? ??
(???? ???? ??) ??? ??? ? ?? ??
?? (??? ?? ???? ? ?) 8192???? ?? ??
???? 10? ?? ???(?? ??):
<?php $filename = "test.txt"; $fh = fopen($filename, "r"); echo fread($fh, "10"); fclose($fh); ?>
file_get_contents()
file_get_contents() ??? ?? ??? ???? ????? ? ?????. ???? ???? ????, ???? FALSE? ?????. .
??:
string file_get_contents( string filename [, int offset [, int maxlen]] )
???? ??:
???? ??
filename ??? ???
offset Optional , ?? ?? ??? ?????. ???? ??? ?? ?????.
maxlen ?? ??, ?? ??? ??? ??? ??? ?????.
?:
<?php $filename = 'NoAlike.txt'; $filestring = file_get_contents($filename); echo $filestring; ?>
fwrite()
fwrite() ??? ??? ???? ?? ????? ??? ?? ?? ???? ? ?????. FALSE? ?????.
??:
int fwrite( ??? ??, ??? ??? [, int ??] )
fwrite()? ???? ??? ?? ??? ??.
???? ??:
???? ??
handle ???? ? ?? ??? , ????? fopen() ???
??? ??? ???
? ?????.length ?? ??, ? ?? ??? ?? ?????.
??? ?? ?? length? ??? ?? length ???? ??? ???? ?? ??? ?????.
?:
<?php // 要寫入的文件名字 $filename = 'file.txt'; // 寫入的字符 $word = "你好!"; $fh = fopen($filename, "w"); echo fwrite($fh, $word); // 輸出:6 fclose($fh); ?>
????? ??? ?????? file.txt ??? ??? ??? ????. Hello!
?? ??? ?? ????? ???? ?? ??? ???? ??? ? ????.
echo fwrite($fh, $word, 4) // ?? : 4
file_put_contents()
file_put_contents() ??? ???? ?? ? ?????. ??? ??, ???? ??? ??? ???? ??? ?? ????, ???? FALSE? ?????.
??:
int file_put_contents(??? ?? ??, ??? ??? [, int ??? [, ??? ????]])
???? ??:
???? ??
filename ???? ? ?? ??
data ? ??????. ??? ???, ??(??? ??? ??) ?? ??? ???? ? ????.
??? ?? ???? ?? ??/?? ??? ?????. ??? ?:
FILE_USE_INCLUDE_PATH: ?? ?? ???? ?? ??? ?????.
FILE_APPEND: ?? ?? ??? ??? ??
LOCK_EX: ??? ????.
context ?? ??, Context? ??? ??? ??? ? ?? ?? ?????.
?:
<?php echo file_put_contents("test.txt", "This is something."); ?>
? ?? ???? ???? ??? ?????. :
18