ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Îļþ´¦Àí

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
ÓÃPHP5µÄDirector..
PHP ×èÖ¹SQL×¢ÈëÊ..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
ÓÃPHP¿ØÖÆÓû§µÄä..
PHP PDO ѧϰ±Ê¼Ç

PHP Îļþ´¦Àí


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-06-17 ä¯ÀÀ:7850 :: ::

The fopen() function is used to open files in PHP.
ÎÒÃÇʹÓÃPHPµÄfopen()º¯ÊýÀ´´ò¿ªÎļþ¡£


Opening a File
ÓÃPHP´ò¿ªÒ»¸öÎļþ

The fopen() function is used to open files in PHP.
¿ÉÒÔʹÓÃPHPµÄfopen()º¯ÊýÀ´´ò¿ªÎļþ¡£

The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:
ÈçÏÂËùʾ£ºº¯ÊýÖеÚÒ»¸ö²ÎÊý°üº¬ÐèÒª±»´ò¿ªµÄÎļþÃû³Æ£¬µÚ¶þ¸ö²ÎÊýÖ¸¶¨ÁË´ò¿ªµÄ·½Ê½£º

<html>
<body>
<?php
$file=fopen("welcome.txt","r"); //ÒÔÖ»¶Á·½Ê½´ò¿ªÁËÓ뵱ǰphpÎļþͬĿ¼ÏµÄwelcome.txtÎļþ
?>
</body>
</html>

The file may be opened in one of the following modes:
´ò¿ªµÄ·½Ê½¾ßÌåÓÐÈçϼ¸ÖÖ£º

Modes
·½Ê½
Description
ÃèÊö
r Read only. Starts at the beginning of the file
Ö»¶Á[Read only]¡£´ÓÎļþµÄ¿ªÍ·´ò¿ª
r+ Read/Write. Starts at the beginning of the file
¿É¶Á/¿Éд[Read/Write]¡£´ÓÎļþµÄ¿ªÍ·´ò¿ª
w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
ֻд[Write only]¡£´ò¿ªÎļþ»òÇå³ýÎļþµÄÄÚÈÝ£¬»òÕß´´½¨Ò»¸ö²»´æÔÚµÄÎļþ
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
¿É¶Á/¿Éд[Read/Write]¡£´ò¿ªÎļþ»òÇå³ýÎļþµÄÄÚÈÝ£¬»òÕß´´½¨Ò»¸ö²»´æÔÚµÄÎļþ
a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
Ìí¼Ó[Append]¡£ÔÚÎļþµÄĩβ´ò¿ªÎļþ»òÊéдÎļþµÄÄÚÈÝ£¬»òÕß´´½¨Ò»¸ö²»´æÔÚµÄÎļþ
a+ Read/Append. Preserves file content by writing to the end of the file
¿É¶Á/Ìí¼Ó[Read/Append]¡£ÏòÎļþÄ©¶ËÊéдÄÚÈÝ£¬±£´æÎļþÄÚÈÝ
x Write only. Creates a new file. Returns FALSE and an error if file already exists
ֻд[Write only]¡£´´½¨Ò»¸öÐÂÎļþ¡£Èç¹ûÎļþÒѾ­´æÔÚÔò·µ»ØFALSE»òÒ»Ìõ´íÎóÐÅÏ¢
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists
¿É¶Á/¿Éд[Read/Write]¡£´´½¨Ò»¸öÐÂÎļþ¡£Èç¹ûÎļþÒѾ­´æÔÚÔò·µ»ØFALSE»òÒ»Ìõ´íÎóÐÅÏ¢

Note: If the fopen() function is unable to open the specified file, it returns 0 (false).
×¢Ò⣺Èç¹ûfopen()²»ÄÜ´ò¿ªÖ¸¶¨Îļþ£¬ÄÇôËû½«·µ»Ø0[flase]¡£

Example
°¸Àý

The following example generates a message if the fopen() function is unable to open the specified file:
µ±fopen()²»ÄÜ´ò¿ªÖ¸¶¨Îļþ£¬¿ÉÒÔ²úÉúÒ»ÌõÐÅÏ¢£¬¾ßÌå´úÂëÈçÏ£º

<html>

<body>
<?php
$file=fopen("welcome.txt","r") or exit("ÎÞ·¨´ò¿ªÎļþ!");
//µ±ÕÒ²»µ½welcome.txtÕâ¸öÎļþʱ¾Í»áÏÔʾÎÞ·¨´ò¿ªÎļþ!
?>
</body>
</html>

 


Closing a File
¹Ø±ÕÎļþ

The fclose() function is used to close an open file:
fclose()º¯Êý¿ÉÓÃÀ´¹Ø±ÕÒÑ´ò¿ªµÄÎļþ£¬ÈçÏ£º

<?php
$file = fopen("test.txt","r");
//Õâ¸öλÖÿÉÒÔÖ´ÐÐһЩ´ò¿ªÎļþºóµÄ´úÂë
fclose($file);
?>

 


Check End-of-file
¼ì²éÊÇ·ñ´¦ÓÚÎļþÄ©¶Ë[End-of-file:EOF]

The feof() function checks if the "end-of-file" (EOF) has been reached.
feof()º¯ÊýÓÃÀ´¼ì²éÊÇ·ñ³öÓÚÎļþÄ©¶Ë£»

The feof() function is useful for looping through data of unknown length.
feof()º¯Êý¶Ô³¤¶Èδ֪µÄÊý¾ÝÖ´ÐÐÑ­»·Óï¾ä·Ç³£ÓÐÓã»

Note: You cannot read from files opened in w, a, and x mode!
×¢Ò⣺Äã²»ÄܶÁÈ¡ÒÔw£¬a£¬x·½Ê½´ò¿ªµÄÎļþ£¡

if (feof($file)) echo "ÎļþµÄ×îºó²¿Î»";

 


Reading a File Line by Line
ÖðÐжÁÈ¡Îļþ

The fgets() function is used to read a single line from a file.
Fgets()º¯ÊýÓÃÀ´¶ÁÈ¡ÎļþÖеÄÒ»ÐÐÄÚÈÝ¡£

Note: After a call to this function the file pointer has moved to the next line.
×¢Ò⣺Õâ¸öº¯Êý±»ÇëÇóÖ®ºó£¬Ëü»á×Ô¶¯Ìøµ½ÏÂÒ»ÐС£

Example
°¸Àý

The example below reads a file line by line, until the end of file is reached:
ÏÂÃæµÄ°¸ÀýչʾÁËÈçºÎÖðÐжÁȡһ¸öÎļþ£¬Ò»Ö±¶Áµ½ÎļþµÄ×îÄ©¶Ë£¬¾ßÌåÈçÏ£º

<?php
$file = fopen("welcome.txt", "r") or exit("ÎÞ·¨´ò¿ªÎļþ!");
//ÖðÐÐÊä³öÎļþÀïµÄÐÅÏ¢£¬Ö±µ½×îºóÒ»ÐнáÊø
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>

 


Reading a File Character by Character
Öð×Ö¶ÁÈ¡Îļþ

The fgetc() function is used to read a single character from a file.
Fgetc()º¯ÊýÊÇÓÃÀ´¶Áȡһ¸öÎļþÖеÄÒ»¸ö×Ö·û¡£

Note: After a call to this function the file pointer moves to the next character.
×¢Ò⣺Õâ¸öº¯Êý±»ÇëÇóÖ®ºó£¬Ëü»á×Ô¶¯Ìøµ½ÏÂÒ»¸ö×Ö·û¡£

Example
°¸Àý

The example below reads a file character by character, until the end of file is reached:
ÏÂÃæµÄ°¸ÀýչʾÁËÈçºÎÖð×Ö¶Áȡһ¸öÎļþ£¬Ò»Ö±¶Áµ½ÎļþµÄ×îºóÒ»¸ö×Ö·û£¬¾ßÌåÈçÏ£º

<?php
$file=fopen("welcome.txt","r") or exit("ÎÞ·¨´ò¿ªÎļþ!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>

 


PHP Filesystem Reference
PHP Filesystem ²ÎÊý

For a full reference of the PHP filesystem functions, visit our PHP Filesystem Reference.
Èç¹ûÏëÁ˽âËùÓеÄfilesystemº¯Êý£¬Çë²ÎÕÕÎÒÃǵÄPHP Filesystem ²ÎÊý¡£

ÆÀÂÛ (0) All