PHP Share (35) file write-in notes

Source: Internet
Author: User

1:resourcefopen( string  $filename Span class= "Apple-converted-space" > , < Span class= "type" >string  $mode  [, < Span class= "type" >bool  $use _include_path  = false  [,resource  $context   ]] )

< Span class= "Apple-converted-space" > < Strong>fopen ()    < Code class= "parameter" >filename   the specified name resource is bound to a stream

When 1:fopen writes, pay attention to the habit plus the third argument.
2: If PHP thinks filename Specifies a local file that will attempt to open a stream on the file. The file must be accessible by PHP, so you need to confirm that the file access permission allows that access. If Safe mode is activated or Open_basedir
The further restrictions apply.
3: If the open fails, will produce an E_WARNING error , so use file_exist to check if the file really exists and have permission to operate, used to add @ block error before
4: Mode
Files that can be automatically created are:
The ' W ' Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
The ' w+ ' read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
' A ' writes open, pointing the file pointer at the end of the file. If the file does not exist, try to create it.
The ' A + ' read-write mode opens, pointing the file pointer at the end of the file. If the file does not exist, try to create it.

If it does not exist, it is created, and the error returns false if present:
' X ' is created and opened in writing, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create it.
This specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent.
' x+ ' is created and opened as read-write, and other behaviors are the same as ' X '.

If the file does not exist, it is created, if the file already exists, then the C mode will not truncate the file size to 0 (like W mode), and will not error (like x mode), the file pointer is set to the file header,
' C ' Open the file for writing only. If The file does not exist, it is created. If it exists, it is neither truncated (as opposed to ' W '),
Nor the call to this function fails (as was the case with ' X '). The file pointer is positioned on the beginning of the file. This could be useful
If it ' s desired to get a advisory lock (see Flock ()) before attempting to modify the file, as using ' W ' could truncate th E file before the
Lock was obtained (if truncation are desired, ftruncate () can be used after the lock is requested).

' c+ ' Open the file for reading and writing; Otherwise it has the same behavior as ' C '.

5: For transplant considerations, it is strongly recommended to use fopen () always use the ' B ' tag when opening a file .

windows provides a text conversion token ( ' t ' ) to transparently &NBSP; \ n Span class= "Apple-converted-space" >  converted to &NBSP; \ r \ n . You can also use   with this counterpart, ' B '   To force the use of binary mode so that no data is converted. To use these tags, either use   ' B '   or   ' t '   as  mode   The last character of the parameter.

The default conversion mode relies on SAPI and the PHP version used, so it is always necessary to specify the appropriate markup for ease of porting. If you are manipulating plain text files and using \ n as the line terminator in the script, but also expect that these files can be read by other applications such as Notepad, use ' t 'in mode. Use ' B 'in all other cases.

If you do not specify a ' B ' tag when working with binaries, you may encounter some strange problems, including broken picture files and strange questions about \ r \ n characters.

6: This function can also be used to open the directory;
This function may also succeed if is filename a directory. If you were unsure whether filename is a file or a directory, you may need
The is_dir () function before calling fopen ().

7: File opened with W mode does not update change time (modification, filemtime), you must use touch to change the time after writing, to ensure that some cache systems will use the modified time to determine
Note-using fopen in ' W ' mode would not update the modification time (filemtime) of a file like you may expect. Want to issue a touch ()
After writing and closing the file which update its modification time. This is become critical in a caching situation, if you intend to keep your hair.

2: bool feof ( resource $handle )

The 1:handle file pointer must be valid and must point to a file that was successfully opened by fopen () or Fsockopen () (and not yet closed by fclose ()). 2: Returns an error (including the socket timeout) if the file pointer goes to EOF or if an error occurs, otherwise returns false. 3: You should use feof money to determine whether handle is valid, because if the passed file pointer is invalid, it may fall into an infinite loop because feof () will not return true to determine the method: if (GetType ($source) = = "Resource") or if (!$ Soure)

PHP Share (35) file write-in notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.