PHP ?? ??
PHP?? ?? ??, ??, ???, ??? ?? ??? ??? ????.
??: ??? ???? ????!
??? ??? ?? ?? ???? ???. ???? ?? ??? ??? ?? ? ????.
???? ??? ??? ????.
1. ??? ?? ??
2. ?? ????? ?? ???? ???
3. ?? ?? ??? ??
PHP readfile() ??
readfile($filename)
??: ?? ??? ???? ??? ??
?? ?? ?? ??? ??? file.txt?? ??? ????.
PHP ??? ?? PHP? ?? ?? ?????
readfile() ??? ?? PHP ??? ???? ? ??? ?? ?? ???? ???? ??? ??? ????(??? ???? readfile() ??? ??? ?? ?????).
??? ? : ?? 1??? ??, gbk ??? ??? 2??? ?? utf-8 ??? 3??? ??, ?? ? ?? 1?
<?php echo readfile("D:WWW/item/file.txt"); ?>
???? ?? ??:
PHP ??? ?? PHP39? ?? ?? ?????
file_get_contents?? ??
? ??? ?? ?? ?????. ??? ? ? ??? ??? ? ?? ?? ??? ????
PHP? ??? ? ??? ?????. ? ???? PHP? ??? ?? ??? ???? ?? ? ?????.
file_get_contents(??? ?? ??)
??: ?? ?? ?? ??? ???? ??? ?? ?? ??? ?????. ??? ??? ??????.
?? ?? ?? ??? ??? file.txt ??? ????.
file_get_contents open? ?????
file_get_contents? ???? ??
<?php $fileName="file.txt"; $filestring = file_get_contents($fileName); echo $filestring; ?>
???? ?? ??:
file_get_contents open? ?????
fopen, fread, fclose ?? ?? ??
fopen ($?? ??, ??)
fread ($?? ???, ?? ??)
fclose ($?? ???)
? ??? ?? ??? ??? ???? ?? ??? ???????.
1. ??? ??
2. ?? ??? ??? ??
3. ??? ??
>fopen ??? ??? ??? ?? ????: 1. ??? ?? ??
2. ??? ???? ? ???? ???? ?? ??? ?????. ?? ???? ? ? ??? ???.
fread
?? ??? ??? ?? ?? ???? ?? ????. ??? ??? ?? ???? ?? ??? ?? ??? ?? ?????. ?? ???.
fclose?? fclose ??? ??? ???? ?? ????. ???? ??? ????.
fopen ??(?? ?):
?? | r |
r+ | |
w ??? ?????. ?? ??? ?? ????. ??? ??? ? ??? ????. | |
??/??. ?? ??? ?? ????. ??? ??? ? ??? ????. |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
a+?> | ??/??. ?? ?? ???? ?? ??? ?????. ?>?> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?> < ??>?> x?>?> | ? ??. ? ??? ????. ??? ?? ???? ?? FALSE? ??? ?????. ?>?> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?> < ??>?> x+?>?> | ?? /??. ? ??? ????. ??? ?? ???? ?? FALSE? ??? ?????. ?>?> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t?> | ??? n? rn?? ???>?> < ??>?>
Instance <?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $filename = 'test.txt'; $fp= fopen($filename, "r"); $len = fwrite($fp, '我是一只來自南方的狼,一直在尋找心中的花姑娘'); fclose($fp); print $len .'字節(jié)被寫入了\n'; ?> ???? ?? ??:
r??? ???? ? ???? ?? ?? ??? ??? ?? ??? ?? ?????. ?? ??? ??? ?? ???? ???? ???? ?? ?????. ?? ?? ??? ?? ?? ?? 1. ?? ? ?? 2. ? ??? ?? ??? ??? ??? ???? A? ?? ?? ??? B? ???? B? ?? ??? C? ?????. ???? ????? ?? ?? ?? ???? B? ?? ?? ?? A? ???? B? ? ? ????. ?? ?? ?? C? ?????. ? ??? ???: resource tmpfile ( ) ??: ?? ??? ???? ??? ??? ?????. ??? ??? ?????. ???? <?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $handle = tmpfile(); //向里面寫入了數(shù)據(jù) $numbytes = fwrite($handle, '寫入臨時文件的內(nèi)容'); //關(guān)閉臨時文件,文件即被刪除 fclose($handle); echo '向臨時文件中寫入了'.$numbytes . '個字節(jié)'; ?> ???? ?? ??:
?? ??, ??, ?? ?? ?? ??? ?? ???($?? ??,$? ??); ? ??? bool ?? ???? ?? ??? ? ???? ?????. ? <?php $fileName1="text.txt"; $fileName2="text--1.txt"; rename($fileName1,$fileName2); ?> ?? ?? text.txt ??? ??? text--1.txt? ??? ????. ?? ?? ??( ?? ?? , ?? ??) ?? : ??? ??? ?? ??? ?? ??? ?? ??? ?????. ?? <?php $filename = 'file.txt'; //舊文件名 $filename2 = 'copy-file.txt'; //新文件名 copy($filename, $filename2); //修改名字。 ?> ? ?? ?? ??? file.txt ??? ???? ????. , ??? ??? ??? copy-file.txt?? ??? ?????. ?? ?? ?? ??(??? ??? ??) ?? <?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $filename = 'test.txt'; unlink($filename); ?> ?? ?? test.txt?? ??? ???? ???? ???? ?? ???? ?? ?? ?? ?? ?? ?? ? <?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $filename = 'file.txt'; echo $filename . '文件大小為: ' . filesize($filename) . ' bytes'; ?> ???? ?? ??:
?? ?? ??, ?? ?? ?? < ??? ?? ="-642"> |
?>?>???>?> | ?? | ?? ??? ??? ????? | fgets | ?? ????? ? ?? ?? ??? ?? ? false? ?? | fgetc | ?? ????? ??? ?? ??? ?? ? false? ?????. | ftruncate | ??? ??? ??? ????. | ?? ?? ??? ???? ?? ??? ?????. text.txt ??? ?? ??? ?????:
fgetc? ??? ????. <?php //以增加的r模式打開 $fp = fopen('text.txt','r+'); //你分發(fā)現(xiàn)每次只讀一個字符 echo fgetc($fp) ."<br>"; //我要全部讀取可以,讀取一次將結(jié)果賦值一次給$string while($string = fgetc($fp)){ echo $string; } ?> ???? ?? ??:
fgets? ? ?? ? ?? ???. <?php //以增加的r模式打開 $fp = fopen('text.txt','r+'); //你分發(fā)現(xiàn)每次只讀一個字符 echo fgets($fp)."<br>"; echo fgets($fp)."<br>"; echo fgets($fp)."<br>"; echo fgets($fp); ?> ????? ?????. ??:
?? ?? ?? <?php //打開我們上面的text.txt文件 $file = fopen("text.txt", "a+"); //你可以數(shù)數(shù)20個字有多長,看看是不是達到效果了 echo ftruncate($file,10); fclose($file); ?> ????? ???? text.txt ??? ??? 20???? ??? ????? ??? ?? ??
Instance <?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $filename = 'text.txt'; if (file_exists($filename)) { echo "$filename"."文件的上次訪問時間是: " . date("Y-m-d H:i:s", fileatime($filename))."<br>"; echo "$filename"."文件的創(chuàng)建時間是: " . date("Y-m-d H:i:s", filectime($filename))."<br>"; echo "$filename"."文件的修改時間是: " . date("Y-m-d H:i:s", filemtime($filename)); } ?> ???? ?? ??:
???? ?? ?? ???? ??? ??? ?? ??, ? ???? ??? ??? ?????? ????? ?? ??? ??? ??? ?????? ?? ??? ?? ????? ??? ????. 1. ?? ??? ??? ? ???? ?? 2. ??? ?? ??? ??? ?? ?? ????? ??? ??? ?? ?????. readdir? ????? ??? ? ? ????. 4. readdir? ???? ??? ????. ?? ? ?? ??? ??? false? ?????. 5. ?? ????? ???? ??? ???? ? ????? ???? ??:
???? <?php //設(shè)置打開的目錄是D盤 $dir = "D:/"; //判斷是否是文件夾,是文件夾 if (is_dir($dir)) { if ($dh = opendir($dir)) { //讀取一次向后移動一次文件夾指針 echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; //讀取到最后返回false //關(guān)閉文件夾資源 closedir($dh); } } ?> ??? ? ????. ???? D ???? ?????? ???? ???? ?? ?? ?? <?php //設(shè)置打開的目錄是D盤 $dir = "D:/"; //判斷是否是文件夾,是文件夾 if (is_dir($dir)) { if ($dh = opendir($dir)) { //讀取到最后返回false,停止循環(huán) while (($file = readdir($dh)) !== false) { echo "文件名為: $file : 文件的類型是: " . filetype($dir . $file) . "<br />"; } closedir($dh); } } ?> ????? ???? ???? ?? ?? ?? ??? ?? ?? ?? ?? ?? ??? ?????. ?: 1. ?? ???? ???? ???. 2. ??? ???? ????? ?? 3. ?? ???? ???? ??? ???? ??? 4. ?? URL? ? ??? ?? ???? ???? ?? ???? 5. ??? ?? URL? ???? ???? ?? ?? ??? ??? ???? ?? ?? ????. ?? ??? ????? ???? ?? ?? ??? ??????.
pathinfo pathinfo (string $path) Instance <?php header("Content-type:text/html;charset=utf-8"); $path_parts = pathinfo('D:/www/a.html'); echo '文件目錄名:'.$path_parts['dirname']."<br />"; echo '文件全名:'.$path_parts['basename']."<br />"; echo '文件擴展名:'.$path_parts['extension']."<br />"; echo '不包含擴展的文件名:'.$path_parts['filename']."<br />"; ?> ???? ?? ??:
basename basename ( string $path[, string $suffix]) Instance <?php echo "1: ".basename("d:/www/a.html", ".d")."<br>"; echo "2: ".basename("d:/www/include")."<br>"; echo "3: ".basename("d:/www/text.txt")."<br>"; ?> ???? ?? ??:
dirname dirname(string $path ) Instance <?php $a=dirname(__FILE__); echo$a; ?>
???? ?? parse_url parse_url (string $path) Instance <?php $url = 'http://username:password@hostname:9090/path?arg=value#anchor'; var_dump(parse_url($url)); ?> ???? ?? ??:
http_build_query http_build_query (??? $data ??) ???? <?php //定義一個關(guān)聯(lián)數(shù)組 $data = [ 'username'=>'liwenkai', 'area'=>'hubei', 'pwd'=>'123' ]; //生成query內(nèi)容 echo http_build_query($data); ?> ???? ?? ??:
PHP ?? ??? ?? ??? PHP ?? ??? ??? ?? ?? ?? ???? ??? PHP ?? ??? ?? ???? ?????. ||
<?php
header("Content-type:text/html;charset=utf-8");
$data = "我是一個兵,來自老百姓";
$numbytes = file_put_contents('binggege.txt', $data);
if($numbytes){
echo '寫入成功,我們讀取看看結(jié)果試試:';
echo file_get_contents('binggege.txt');
}else{
echo '寫入失敗或者沒有權(quán)限,注意檢查';
}
?>
?? ????? ????? ? ????. ?? ???? ???? ????. ???? ? ??? ?? ?? ??????~
? ??? ??? ???? ???? ????.
|