Home > Backend Development > PHP Problem > How to clear file contents in php

How to clear file contents in php

青灯夜游
Release: 2023-03-11 14:34:01
Original
3216 people have browsed it

In PHP, you can use the file_put_contents() function to clear the file contents. This function can write a string into the file. If the "FILE_APPEND" value is not set, the file contents will be cleared; syntax " file_put_contents("file path","")".

How to clear file contents in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use file_put_contents () function to clear the file contents.

The file_put_contents() function writes a string to a file. The syntax format is:

file_put_contents(file,data,mode,context)
Copy after login
##context Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream.
ParameterDescription
fileRequired. Specifies the file to which data is to be written. If the file does not exist, a new file is created.
dataRequired. Specifies the data to be written to the file. Can be a string, array, or data stream.
modeOptional. Specifies how to open/write the file. Possible values:
  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX
When this function accesses a file, it follows the following rules:


  • If FILE_USE_INCLUDE_PATH is set, *filename will be checked * Built-in path to copy

  • If the file does not exist, a file will be created

  • Open the file

  • If LOCK_EX is set, the file will be locked

  • If FILE_APPEND is set, it will be moved to the end of the file. Otherwise, the contents of the file will be cleared

  • Write data to the file

  • Close the file and unlock all files

  • If successful, this function returns the number of characters written to the file. On failure, False is returned.

Return value: If successful, this function will return the number of characters written to the file. On failure, False is returned.

Example: Clear file content

<?php
echo file_put_contents("sites.txt","");
?>
Copy after login

Output:

0
Copy after login
Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to clear file contents in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template