PHP file_put_contents 将字符串写入或追加到文件
在php中文件file_put_contents函数是可以把我们字符串写入到文件中哦,这个与php fwrite文件有一点相同了,下面我来看看看file_put_contents用法与fwrite区别。
PHP file_put_contents() 函数是一次性向文件写入字符串或追加字符串内容的最合适选择。
file_put_contents()
file_put_contents() 函数用于把字符串写入文件,成功返回写入到文件内数据的字节数,失败则返回 FALSE
例子:
代码如下 | 复制代码 |
echo file_put_contents("test.txt", "This is something."); |
运行该例子,浏览器输出:
18
而 test.txt 文件(与程序同目录下)内容则为:This is something.。
提示
•如果文件不存在,则创建文件,相当于fopen()函数行为。
•如果文件存在,默认将清空文件内的内容,可设置 flags 参数值为 FILE_APPEND 以避免(见下)。
•本函数可安全用于二进制对象。
以追加形式写入内容
当设置 flags 参数值为 FILE_APPEND 时,表示在已有文件内容后面追加内容的方式写入新数据:
代码如下 | 复制代码 |
file_put_contents("test.txt", "This is another something.", FILE_APPEND); |
执行程序后,test.txt 文件内容变为:This is something.This is another something.
file_put_contents() 的行为实际上等于依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
那么到底file_put_contents与fwrite区别在哪里
如下为file_put_contents的实例代码:
代码如下 | 复制代码 |
$filename = 'file.txt'; |
同样的功能使用fwrite的实例代码:
代码如下 | 复制代码 |
$filename = 'file.txt'; |
从以上两个例子看出,其实file_put_contents是fopen、fwrite、fclose三合一的简化写法,这对程序代码的优化是有好处的,一方面在代码量上有所减少,另一方面不会出现fclose漏写的不严密代码,在调试、维护上方便很多。
上述例子里,file_put_contents是从头写入,如果要追加写入,怎么办呢?
在file_put_contents的语法里,有个参数FILE_APPEND,这是追加写入的声明。实例代码如下:
代码如下 | 复制代码 |
echo file_put_contents('file.txt', "This is another something.", FILE_APPEND); |
FILE_APPEND就是追加写入的声明。在追加写入时,为了避免其他人同时操作,往往需要锁定文件,这时需要加多一个LOCK_EX的声明,写法如下:
代码如下 | 复制代码 |
echo file_put_contents('file.txt', "This is another something.", FILE_APPEND|LOCK_EX); |
注意,以上代码中echo输出到显示器里的是写入文件字符串的长度

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

The form form in HiddenHttpMethodFilterhtml only supports GET and POST requests, but methods such as DELETE and PUT are not supported. Spring 3 adds a filter that can convert these requests into standard http methods, so that GET, POST, PUT, and DELETE requests are supported. @BeanpublicFilterRegistrationBeantestFilterRegistration3(){FilterRegistrationBeanregistration=newFilterRegistrationBea
