Home Backend Development PHP Tutorial Detailed example of php fwrite() function appending and newline writing

Detailed example of php fwrite() function appending and newline writing

Jun 01, 2017 am 10:35 AM

PHP fwrite() function

fwrite() function is used to write a string to a file and returns the number of characters written successfully, otherwise it returns FALSE.

Syntax:

int fwrite( resource handle, string string [, int length] )
Copy after login

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

Parameter description:


Parameter Description
handleRequired. Specifies the open file to write to. Generally created by the fopen() function
stringRequired. Specifies the string to be written to the open file.
length Optional. Specifies the maximum number of bytes to be written.


ps: If the optional parameter length is specified, when length bytes are written or after the string is written , writing will stop.

Example

<?php
// 要写入的文件名字
$filename = &#39;file.txt&#39;;
// 写入的字符
$word = "你好!";
$fh = fopen($filename, "w");
echo fwrite($fh, $word);    // 输出:6
fclose($fh);
?>
Copy after login

Execute this example program. In the same directory as the program, the content of the file.txt file is: Hello!

Use the length parameter

In the above example, if the length parameter is used, at most length strings will be written:

echo fwrite($fh, $word, 4);    // 输出:4
Copy after login

PHP fwrite append writing

The append writing to the file actually has nothing to do with the fwrite function, but is related to the mode of opening the file by the fopen function. When fopen opens a file, the mode parameter selects a, which means appending to the file:

<?php
$filename = &#39;file.txt&#39;;
$word = "你好!";

$fh = fopen($filename, "a");
echo fwrite($fh, $word);
fclose($fh);
?>
Copy after login

PHP fwrite newline writing

If you want to implement it in the file To write with a newline, you only need to add the newline character n where a newline is needed in the written content:

<?php
$filename = &#39;file.txt&#39;;
$word = "你好!n";

$fh = fopen($filename, "a");
echo fwrite($fh, $word);
fclose($fh);
?>
Copy after login

The above example adds n newline characters at the end of the content. To make n represent a newline when writing a file, you need to use double quotes when writing (as in the example above). If you use single quotes, the n character will not be interpreted as a newline but as an n string. Click to view: PHP string The difference between single quotes and double quotes.

n is already a real line feed. If you want to simulate a carriage return and line feed on the Windows operating system (that is, when you open a file with WordPad, it will not be a black square but listed line by line). You can use n Add r and carriage return in front:

<?php
$word = "你好!rn";
?>
Copy after login

PHP fwrite write permission

When opening an existing file (usually in append writing mode), it is necessary Check whether the file has write permission to avoid a system error. Use the is_writable function to check whether the file is writable.

The following is an example of strict checking in append writing mode. The code is as follows:

<?php
$filename = &#39;file.txt&#39;;
$word = "你好!n";

// 确定文件存在并且可写
if (is_writable($filename)) {
    //打开文件
    if (!$fh = fopen($filename, &#39;a&#39;)) {
         echo "不能打开文件 $filename";
         exit;
    }
    // 写入内容
    if (fwrite($fh, $word) === FALSE) {
        echo "不能写入到文件 $filename";
        exit;
    }
    echo "成功地将 $word 写入到文件 $filename";
    fclose($fh);
} else {
    echo "文件 $filename 不可写";
}
?>
Copy after login

PHP fwrite function only performs the action of writing strings to the file. The actual result of its behavior depends on More about the fopen function.

【Recommended related articles】:

1.Detailed explanation of the php fwrite() function writing file example

2 .What should I do if the newline character does not work when the php fwrite() function writes newlines?

The above is the detailed content of Detailed example of php fwrite() function appending and newline writing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to wrap rows in excel table How to wrap rows in excel table Feb 09, 2024 pm 02:00 PM

In the process of using Excel, we have to perform many different operations. Sometimes we need to wrap a line in a cell. So what exactly do we need to do to wrap a cell? Let’s take a look at how to wrap lines in a computer excel table. [Collection of excel table operation methods] How to wrap rows in excel table? Answer: You can do this by automatically wrapping rows and setting cell formats. 1. Automatic line wrapping 1. We select the area that needs to be wrapped in the table and click [Automatically wrap] on the [Start] page; 2. Then we adjust the width of column A to an appropriate value; 2. Set the cells Format 1. First, we select the area that needs to be wrapped, right-click the mouse, and click [Set Cell Format]; 2. Then in the pop-up

How to wrap lines in BarTender - How to wrap lines in BarTender How to wrap lines in BarTender - How to wrap lines in BarTender Mar 05, 2024 pm 07:52 PM

Many users use the BarTender software in their offices. Recently, some new users have asked how to wrap lines in BarTender. Below, the editor will bring you the method of wrapping lines in BarTender. Let us take a look below. 1. In BarTender, click the Create Text button in the toolbar, select Create Single Line Text, and enter the text content. 2. Double-click the created text object to open the text properties dialog box. Switch to the "Text Format" tab and select "Paragraph" for "Type" on the right. 3. Click Close, adjust the size of the text box or enter more text, or wrap the text according to actual requirements.

How to wrap cells in Excel on Apple computers How to wrap cells in Excel on Apple computers Feb 18, 2024 pm 08:39 PM

Wrapping lines in cells in Apple's Excel Apple's Excel software is a powerful spreadsheet tool that provides many convenient functions to help users with data processing and analysis. When using Excel, sometimes we need to enter multiple lines of text in cells to better organize and present the data. However, since Excel for Apple computers is slightly different from the Windows version of Excel, the method of wrapping lines is also different. In the Windows version of Excel, we can directly

How to output text with line breaks in Go language How to output text with line breaks in Go language Mar 15, 2024 pm 04:15 PM

Go language is a modern, efficient and concise programming language that is widely used in software development in various fields. In the Go language, outputting text with newlines is very simple and can be achieved by using the Println function provided by the fmt package. Below we will introduce in detail how to output text with line breaks in Go language, as well as related code examples. In the Go language, if you want to output text with newlines, you can use the Println function provided by the fmt package. The Println function will output text in

How to wrap line on iPhone? How to set line wrap on iPhone? How to wrap line on iPhone? How to set line wrap on iPhone? Mar 22, 2024 pm 06:50 PM

1. Press the space bar multiple times, move the cursor to the far right, and then enter text to wrap the line. 2. When you need to break a line, click the microphone icon on the keyboard and the voice will say [Line break]. 3. In the memo usage scenario, there is a [Line Break] option in the input method, which can be used to achieve line break by copying. 4. Download other third-party input methods from the app store to use.

PyCharm tutorial: quickly master the automatic line wrapping function PyCharm tutorial: quickly master the automatic line wrapping function Feb 23, 2024 am 08:33 AM

PyCharm is a powerful Python integrated development environment, and its automatic line wrapping function can help developers write code more efficiently. This article will introduce how to quickly master the automatic word wrapping function in PyCharm and provide specific code examples. 1. Enable the automatic word wrapping function. In PyCharm, the automatic word wrapping function is not enabled by default and needs to be manually set before it can be used. The specific steps are as follows: Open PyCharm and enter File->Settings;

How to use fopen, fwrite and fclose in php for file operations? How to use fopen, fwrite and fclose in php for file operations? Jun 01, 2023 am 08:46 AM

In PHP development, file operations are very common. Under normal circumstances, we need to perform file reading, writing, deletion and other operations. Among them, the fopen function and fread function can be used to read the file, and the fopen function, fwrite function and fclose function can be used to write the file. This article will introduce how PHP uses fopen, fwrite and fclose to perform file operations. 1. fopen function The fopen function is used to open files. Its syntax is as follows: r

Detailed explanation of how to replace newlines in PHP Detailed explanation of how to replace newlines in PHP Mar 20, 2024 pm 01:21 PM

Detailed explanation of how to replace newlines in PHP In PHP development, sometimes we need to replace or process newlines in strings. Line breaks may be expressed differently on different platforms, so they need to be processed uniformly to ensure that strings display consistently in different environments. This article will introduce in detail how to replace newlines in PHP, including common newline characters and specific code examples. 1. Common newline characters In different operating systems, the representation of newline characters may be slightly different. The main newline characters include: Windo

See all articles