´´½¨ÎļþÎÒÃÇÓõ½º¯Êý
fopen ( string filename, string mode )
²ÎÊýfilenameΪ´´½¨ÎļþÃu
²ÎÊýmodeΪÒÔʲô*½Ê½´o¿ªÎļþfilename
ÆaÖÐmode¿ÉÄÜÖµÁбi£º
mode ˵Ã÷
¡®r' Ö»¶Á½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþÍ¡£
¡®r+' ¶Áд½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþÍ¡£
¡®w' дÈe½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþͲ¢½«Îļþ´oС½ØΪÁa¡£Èç¹uÎļþ²»´aeÔÚÔo³¢ÊÔ´´½¨Ö®¡£
¡®w+' ¶Áд½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþͲ¢½«Îļþ´oС½ØΪÁa¡£Èç¹uÎļþ²»´aeÔÚÔo³¢ÊÔ´´½¨Ö®¡£
¡®a' дÈe½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþÄ(C)β¡£Èç¹uÎļþ²»´aeÔÚÔo³¢ÊÔ´´½¨Ö®¡£
¡®a+' ¶Áд½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþÄ(C)β¡£Èç¹uÎļþ²»´aeÔÚÔo³¢ÊÔ´´½¨Ö®¡£
¡®x' ´´½¨²¢ÒÔдÈe½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþÍ¡£Èç¹uÎļþÒÑ´aeÔÚ£¬Ôo fopen() µ÷ÓÃʧ°Ü²¢µ»Ø FALSE£¬²¢Éu³ÉÒ»Ìo E_WARNING ¼¶±ðµÄ´iÎoÐÅÏ¢¡£Èç¹uÎļþ²»´aeÔÚÔo³¢ÊÔ´´½¨Ö®¡£ÕaºÍ¸ø µ×²aµÄ open(2) ϵͳµ÷ÓÃÖ¸¶¨ O_EXCL|O_CREAT ±e¼ÇÊǵȼ۵ġ£´ËÑ¡Ïi±» PHP 4.3.2 ÒÔ¼°ÒÔºoµÄ°ae±¾ËuÖ§³Ö£¬½oÄÜÓÃÓÚ±¾µØÎļþ¡£
¡®x+' ´´½¨²¢ÒÔ¶Áд½Ê½´o¿ª£¬½«ÎļþÖ¸ÕeÖ¸ÏoÎļþÍ¡£Èç¹uÎļþÒÑ´aeÔÚ£¬Ôo fopen() µ÷ÓÃʧ°Ü²¢µ»Ø FALSE£¬²¢Éu³ÉÒ»Ìo E_WARNING ¼¶±ðµÄ´iÎoÐÅÏ¢¡£Èç¹uÎļþ²»´aeÔÚÔo³¢ÊÔ´´½¨Ö®¡£ÕaºÍ¸ø µ×²aµÄ open(2) ϵͳµ÷ÓÃÖ¸¶¨ O_EXCL|O_CREAT ±e¼ÇÊǵȼ۵ġ£´ËÑ¡Ïi±» PHP 4.3.2 ÒÔ¼°ÒÔºoµÄ°ae±¾ËuÖ§³Ö£¬½oÄÜÓÃÓÚ±¾µØÎļþ¡£
ÏÂÃae´uÂeÊÇÎÒͨ¹ýÀuÓú¯Êýfopen´´½¨Ò»¸oÃuΪDemo£¬¸ñʽΪtxtµÄÎļþ£¬²¢ÔÚÎļþÖÐдÈe"Welcome To ItCodeWorld!"µÄÊý¾Ý¡£
< ?php
//Òª´´½¨µÄÁ½¸oÎļþ
$TxtFileName = "Demo.txt";
//ÒÔ¶Áд*½Ê½´oдָ¶¨Îļþ£¬Èç¹uÎļþ²»´aeÔo´´½¨
if( ($TxtRes=fopen ($TxtFileName,"w+")) === FALSE){
echo("´´½¨¿ÉдÎļþ£º".$TxtFileName."ʧ°Ü");
exit();
}
echo ("´´½¨¿ÉдÎļþ".$TxtFileName."³É¹¦£¡</br>");
$StrConents = "Welcome To ItCodeWorld!";//Òª д½øÎļþµÄÄÚÈÝ
if(!fwrite ($TxtRes,$StrConents)){ //½«ÐÅϢдÈeÎļþ
echo ("³¢ÊÔÏoÎļþ".$TxtFileName."дÈe".$StrConents."ʧ°Ü£¡");
fclose($TxtRes);
exit();
}
echo ("³¢ÊÔÏoÎļþ".$TxtFileName."дÈe".$StrConents."³É¹¦£¡");
fclose ($TxtRes); //¹Ø±ÕÖ¸Õe
?>
PHP ´´½¨Îļþ ¨C fopen()
fopen() º¯ÊýÒ²ÓÃÓÚ´´½¨Îļþ¡£Ò²ÐiÓеa»iÂÒ£¬µ«ÊÇÔÚ PHP ÖУ¬´´½¨ÎļþËuÓõĺ¯ÊýÓe´o¿ªÎļþµÄÏaͬ¡£
Èç¹uÄuÓà fopen() ´o¿ª²¢²»´aeÔÚµÄÎļþ£¬´Ëº¯Êý»a´´½¨Îļþ£¬¼Ù¶¨Îļþ±»´o¿ªÎªÐ´Èe£¨w£(C)»oÔo¼Ó£¨a£(C)¡£
ÏÂÃaeµÄÀý×Ó´´½¨ÃuΪ "testfile.txt" µÄÐÂÎļþ¡£´ËÎļþ½«±»´´½¨ÓÚ PHP ´uÂeËuÔÚµÄÏaͬĿ¼ÖУº
ʵÀý
$myfile = fopen("testfile.txt", "w")
PHP ÎļþȨÏÞ
Èç¹uÄuÊÔͼÔËÐÐÕa¶Î´uÂeʱ¢Éu´iÎo£¬Çe¼i²eÄuÊÇñÓÐÏoÓ²ÅÌдÈeÐÅÏ¢µÄ PHP ÎļþÃÎÊȨÏÞ¡£
PHP дÈeÎļþ ¨C fwrite()
fwrite() º¯ÊýÓÃÓÚдÈeÎļþ¡£
fwrite() µÄµÚÒ»¸o²ÎÊý°uº¬ÒªÐ´ÈeµÄÎļþµÄÎļþÃu£¬µÚ¶þ¸o²ÎÊýÊDZ»Ð´µÄ×Öu´®¡£
ÏÂÃaeµÄÀý×Ó°ÑÐÕÃuдÈeÃuΪ "newfile.txt" µÄÐÂÎļþÖУº
ʵÀý
<?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);
?>
Çe×¢Òa£¬ÎÒÃÇÏoÎļþ "newfile.txt" дÁËÁ½´Î¡£ÔÚÿ´ÎÎÒÃÇÏoÎļþдÈeʱ£¬ÔÚÎÒÃÇ¢Ë͵Ä×Öu´® $txt ÖУ¬µÚÒ»´Î°uº¬ "Bill Gates"£¬µÚ¶þ´Î°uº¬ "Steve Jobs"¡£ÔÚдÈeÍe³Éºo£¬ÎÒÃÇʹÓà fclose() º¯ÊýÀ´¹Ø±ÕÎļþ¡£
Èç¹uÎÒÃÇ´o¿ª "newfile.txt" Îļþ£¬ËuÓ¦¸ÃÊÇÕaÑuµÄ£º
Bill Gates
Steve Jobs
PHP ¸²¸Ç£¨Overwriting£(C)
Èç¹uÏÖÔÚ "newfile.txt" °uº¬ÁËÒ»Ð(C)Êý¾Ý£¬ÎÒÃÇ¿ÉÒÔչʾÔÚдÈeÒÑÓÐÎļþʱ*¢ÉuµÄµÄÊÂÇe¡£ËuÓÐÒÑ´aeÔÚµÄÊý¾Ý»a±»²Á³ý²¢ÒÔÒ»¸oÐÂÎļþ¿ªÊ¼¡£
ÔÚÏÂÃaeµÄÀý×ÓÖУ¬ÎÒÃÇ´o¿ªÒ»¸oÒÑ´aeÔÚµÄÎļþ "newfile.txt"£¬²¢ÏoÆaÖÐдÈeÁËÒ»Ð(C)ÐÂÊý¾Ý£º
ʵÀý
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Èç¹uÏÖÔÚÎÒÃÇ´o¿ªÕa¸o ¡°newfile.txt¡± Îļþ£¬Bill ºÍ Steve ¶¼ÒÑÏuʧ£¬Ö»Ê£ÏÂÎÒÃǸÕдÈeµÄÊý¾Ý£º
Mickey Mouse
Minnie Mouse
»»ÐÐu¡®\n'ºÍ»Ø³µu¡®\r'
¹ËÃu˼Òa£¬»»ÐÐu¾ÍÊÇÁiÆðÒ»ÐУ¬»Ø³µu¾ÍÊǻص½Ò»ÐеĿªÍ£¬ËuÒÔÎÒÃÇƽʱ±aдÎļþµÄ»Ø³µuÓ¦¸ÃÈÇÐÀ´Ëµ½Ð×o»Ø³µ»»ÐÐu
¡®\n' 10 »»ÐУ¨newline£(C)
¡®\r' 13 »Ø³µ£¨return£(C)
Ò²¿ÉÒÔ±iʾΪ'\x0a'ºÍ'\x0d'.(16½øÖÆ)
ÔÚwindowsϵͳÏ£¬»Ø³µ»»ÐÐuºÅÊÇ¡±\r\n¡±.µ«ÊÇÔÚLinuxµÈϵͳÏÂÊÇûÓС±\r¡±uºÅµÄ¡£
ÔÚ½aÎoÎı¾»oÆaËu¸ñʽµÄÎļþÄÚÈÝʱ£¬³£³£ÒªÅoµ½Åж¨»Ø³µ»»ÐеĵØ*½£¬Õa¸oʱºo¾ÍҪעÒa¼ÈÒªÅж¨¡±\r\n¡±ÓÖÒªÅж¨¡±\n¡±¡£
д³ÌÐoʱ¿ÉÄܵõ½Ò»ÐÐ,½«Æa½øÐÐtrimµo'\r',ÕaÑuÄܵõ½ÄaËuÐeÒªµÄstringÁË¡£