PHP ?? ??
PHP ?? ??
??? ???
??? ???? ????? ? ?? ?? ??? ?? ?? ?????????.
?? ???? ??????? ??????? ???? ???? ?? ??? ??? ?? ?? ????? ?? ???? ??? ?????.
? ?? ?? ??
?? ??? ???? ???
???? ??? ????
?? ??
??? ????? ???? ???? ?? ????? ? ?? ?? ??? ????. ??? ??? ????? ?? ??? ??? ???.
? ??? ?? FOPEN ??? ?????.
? ??? ?? FWRITE ??? ?????.
? ??? ????. , FCLose ?? ??
Open File
fopen() ??? PHP?? ??? ?? ? ?????.
? ??? ? ?? ?????? ??? ??? ??? ???? ? ?? ????? ??? ?? ? ??? ??? ?????.
<html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html>
??? ???? ? ? ????. ?? ??:
??: fopen() ??? ??? ??? ? ? ??? 0(false)? ?????.
?
fopen() ??? ??? ??? ? ? ?? ?? ?? ?? ???? ?????:
<html> <body> <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?> </body> </html>
?? ??
fwrite() ??? ??? ?? ? ?????
fwrite?? ? ?? ?? ????? ????. handler? fopen? ????? ?? ? ???? Resource ???? string? ? ??????
int fwrite (resource $handle , string $string [, int $length ] )
??? ???? fwrite? ? ?? ?? ?????. ??? ???? false? ?????. ??>
?<?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = "Bill Gates\n"; fwrite($myfile, $txt); $txt = "Steve Jobs\n"; fwrite($myfile, $txt); fclose($myfile); ?>"newfile.txt" ??? ? ? ????. ??? ? ??? ??? ??? $txt?? ???? "Bill Gates"? ???? ? ???? "Steve Jobs"? ?????. ??? ??? ? fclose() ??? ???? ??? ????. "newfile.txt" ??? ?? ??? ????: Bill GatesSteve Jobs
?? file
fread? while ?? ???? ?? ?? ??? ?? ? ?????. ? ?? ????? ?? ?? ??? ?? ???? ??? ?????. string fread(resource $handle, int $length)??echo fread($resource, 1024); ?? 1024?? ?? ? ????. ?? ??? ????? ??
fclose() ??? ?? ??? ?? ? ?????.<?php $file = fopen("test.txt","r"); //執(zhí)行一些代碼 fclose($file); ?>
? ?? ??? ?(EOF)
feof() ??? ??? ?(EOF)? ????? ??? ?????.feof() ??? ??? ? ? ?? ???? ??? ? ?????.
??: w, a ? x ????? ?? ?? ??? ?? ? ????!
if (feof($file)) echo "?? ?";
??? ? ?? ??
fgets() ??? ??? ????. ???? ? ?? ??? ????.
??: ? ??? ???? ?? ???? ?? ?? ?????.
?
?? ???? ??? ?? ??? ??? ? ?? ????.
<?php $file = fopen("welcome.txt", "r") or exit("無(wú)法打開文件!"); // 讀取文件每一行,直到文件結(jié)尾 while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file); ?>
??? ???? ????
fgetc() ??? ???? ???? ??? ?? ? ?????.
??: ? ??? ???? ?? ???? ?? ??? ?????.
?
?? ?? ??? ?? ??? ??? ? ??? ????.
<?php $file=fopen("welcome.txt","r") or exit("無(wú)法打開文件!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?>
file_put_contents
?? ??? ???, fopen ? fclose? ?? ???? ?????. PHP5?? ??? ??
int file_put_contents ( string $filename , mix $data [, int $flags = 0 [, resources $ context ]] )
file_put_contents? fopen + fwrite + fclose? ???? ?? ????
file_put_contents('file', "hello worldn");
file_put_contents('file', "hello worldn ", FILE_APPEND);
file_get_contents
string file_get_contents( string $filename [, bool $use_include_path = false [, resources$context [, int $offset = -1 [, int $maxlen ]]]] )
file_get_contents? fopen + fread + fclose
echo file_get_contents('file');
??? ???? ?? ????. ?? ?? ??
S file_exists? ?? ??? ?? ??? ???? ??? ????? ??? ??? ?? true ?? false
S filesize? ?? ??? ????, ??? ??
S unlink? ??? ?? ??? ???? ??? ??? ? ????
PHP ?? ??? ?? ???
For PHP ?? ??? ??? ?? ?? ?? ???? ??? PHP ?? ??? ?? ???? ?????.