w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Îļþ´¦Àí
The fopen() function is used to open files in PHP.
ÎÒÃÇʹÓÃPHPµÄfopen()º¯ÊýÀ´´ò¿ªÎļþ¡£
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> <?php </body> |
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]¡£
The following example generates a message if the fopen() function is unable to open the specified file:
µ±fopen()²»ÄÜ´ò¿ªÖ¸¶¨Îļþ£¬¿ÉÒÔ²úÉúÒ»ÌõÐÅÏ¢£¬¾ßÌå´úÂëÈçÏ£º
<html> <?php </body> |
The fclose() function is used to close an open file:
fclose()º¯Êý¿ÉÓÃÀ´¹Ø±ÕÒÑ´ò¿ªµÄÎļþ£¬ÈçÏ£º
<?php //Õâ¸öλÖÿÉÒÔÖ´ÐÐһЩ´ò¿ªÎļþºóµÄ´úÂë fclose($file); |
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 "ÎļþµÄ×îºó²¿Î»"; |
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.
×¢Ò⣺Õâ¸öº¯Êý±»ÇëÇóÖ®ºó£¬Ëü»á×Ô¶¯Ìøµ½ÏÂÒ»ÐС£
The example below reads a file line by line, until the end of file is reached:
ÏÂÃæµÄ°¸ÀýչʾÁËÈçºÎÖðÐжÁȡһ¸öÎļþ£¬Ò»Ö±¶Áµ½ÎļþµÄ×îÄ©¶Ë£¬¾ßÌåÈçÏ£º
<?php |
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.
×¢Ò⣺Õâ¸öº¯Êý±»ÇëÇóÖ®ºó£¬Ëü»á×Ô¶¯Ìøµ½ÏÂÒ»¸ö×Ö·û¡£
The example below reads a file character by character, until the end of file is reached:
ÏÂÃæµÄ°¸ÀýչʾÁËÈçºÎÖð×Ö¶Áȡһ¸öÎļþ£¬Ò»Ö±¶Áµ½ÎļþµÄ×îºóÒ»¸ö×Ö·û£¬¾ßÌåÈçÏ£º
<?php |
For a full reference of the PHP filesystem functions, visit our PHP Filesystem Reference.
Èç¹ûÏëÁ˽âËùÓеÄfilesystemº¯Êý£¬Çë²ÎÕÕÎÒÃǵÄPHP Filesystem ²ÎÊý¡£
ÆÀÂÛ (0)
All