Home  >  Article  >  Backend Development  >  How to clear file contents in php

How to clear file contents in php

青灯夜游
青灯夜游Original
2021-07-14 18:27:363167browse

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)
##context Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream.
Parameter Description
file Required. Specifies the file to which data is to be written. If the file does not exist, a new file is created.
data Required. Specifies the data to be written to the file. Can be a string, array, or data stream.
mode Optional. 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","");
?>

Output:

0

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!

Statement:
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