Php file read and write code (fopen, is_writable, fwrite)

Source: Internet
Author: User
Tags php tutorial php web server

Php Tutorial file operations, including file functions fopen, is_writable, fwrite, and fclose. Let's take a look at the instance.

<? Php
$ Filename = "html/cache.txt ";
$ Contents = "I am Zhang Bin ";
If (is_writable ($ filename )){
If ($ handle = fopen ($ filename, "a") = false ){
Echo "failed to write file $ filename ";
Exit ();
}
If (fwrite ($ handle, $ contents) = false ){
Echo "failed to write file $ filename ";
Exit ();
}
Echo "the file $ filename is successfully written ";

Fclose ($ handle );
} Else {
Echo "file $ filename cannot be written ";
}
?>

The fopen () function opens a file or URL.
If opening fails, this function returns FALSE.
Syntax
Fopen (filename, mode, include_path, context)
Parameter description
Filename is required. Specifies the file or URL to open.
Mode is required. Specifies the access type of the file/stream. Possible values are shown in the table below.
Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE.
Context is optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream.

Definition and usage
The fwrite () function writes data to a file (which can be safely used for binary files ).
Syntax
Fwrite (file, string, length)
Parameter description
File is required. Specifies the open file to be written.
String is required. Specifies the string of the file to be written.
Length is optional. Specifies the maximum number of bytes to write.
Description
Fwrite () writes the string content to the file pointer. If length is specified, the write will stop after the length byte is written or the string is written.
Fwrite () returns the number of written characters. If an error occurs, false is returned.

<? Php
$ File = fopen ("test.txt", "w ");
Echo fwrite ($ file, "Hello World. Testing! ");
Fclose ($ file );
?>

Is_writable
(PHP 4 and PHP 5)

Is_writable-tell whether filename is written

Description
Boolean is_writable (string $ file name)
Returns TRUE if the object exists and can be written. This file name parameter may be a directory name that allows you to check if it is a writable directory.

Remember that the PHP Web server that can access the file's user id runs (usually 'Who '). The limitation of the security mode is not taken into account.

<? Php
$ Filename = 'test.txt ';
If (is_writable ($ filename )){
Echo 'The file is writable ';
} Else {
Echo 'The file is not writable ';
}
?>

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.