PHP File fwrite


fwrite() function is used to write a string to the file, the successful return of the number of characters written, otherwise return FALSE.

grammar:

int fwrite(resource handle, string string[, int length])

fwrite() writes the contents of string to the file pointer handle.

Parameter Description:
Parameters Description
handle The file pointer to write to the string, typically created by the fopen() function
data the string to be written
length Optional, specifies the maximum number of bytes to write

if the optional argument length is specified, writing will stop when length bytes are written or after string has been written.

example:

// The name of the file to be written
$filename='file.txt';
// Write characters
$word="Hello!";

$fh=fopen($filename," w ");
echo fwrite($fh, $word); // Output: 6
fclose($fh);

The implementation of the sample program, the same directory in the program, file.txt file content: Hello!


Use the length parameter

In the example above, if the length parameter is used, up to length strings are written:

echo fwrite($fh, $word, 4); // Output: 4

PHP fwrite append

Additional writing to the document, the actual function has nothing to do with the fwrite, and fopen function to open the file on the model. When fopen to open the file, mode parameter selection a, then add the file after the write:

$filename='file.txt';
$word="Hello!";

$fh=fopen($filename," a ");
echo fwrite($fh, $word);
fclose($fh);

PHP fwrite line-feed

if you want to write in the file for the new line, just write the contents of the need to add a newline where the line n can be:

$filename='file.txt';
$word="Hello! N";;

$fh=fopen($filename," a ");
echo fwrite($fh, $word);
fclose($fh);

The above example adds an n linefeed at the end of the content. if you use single quotation marks, n characters will not be interpreted as a new line but as n string, click to view: The difference between single and double quotation marks in PHP strings.

N is a real sense of the line, in the windows operating system if you want to simulate the carriage return line(that is, open the file with WordPad, not a black box but a line of his list), you can add r in front of r :

$word="Hello! Rn";;

PHP fwrite write permission

When an existing file is opened(usually in an append write mode), it is necessary to detect whether the file has write privileges, thus avoiding a system error. Use the is_writable function to detect whether the file is writable.

The following is an example of a stricter check in append mode:

$filename='file.txt';
$word="Hello! N";;

// Make sure the file exists and is writable
if (is_writable($filename)) {
    // open a file
    if (! $fh=fopen($filename, 'a')) {
         echo "can not open the file $filename";
         exit;
    }
    // Write the contents
    if (fwrite($fh, $word) === FALSE) {
        echo "can not write to file $filename";
        exit;
    }
    echo "successfully writes $word to the file $filename";
    fclose($fh);
} else {
    echo "file $filename can not be written";
}

PHP fwrite function is only the implementation of the document to write string action, the actual outcome of its behavior depends on the fopen function more. The file_put_contents function may be a better choice if you want to write or append a string to a file at once.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook