Home >Backend Development >PHP Tutorial >Sharing steps for writing files in php

Sharing steps for writing files in php

WBOY
WBOYOriginal
2016-07-25 08:56:481514browse
This article introduces the steps to write files in PHP programming. Friends who need it can learn it.

In PHP, writing files generally requires the following steps: 1. First, determine the content to be written to the file $content = ‘Hello’;

2. Then, open the file (the system will automatically create this empty file)

//假设新建的文件叫file.txt,而且在上级目录下。w表示‘写文件’,
$fp下面要用到,表示指向某个打开的文件。
$fp = fopen(’../file.txt’, ‘w’);

3. Write the content string to the file //$fp tells the system the file to be written, and the written content is $content.

fwrite($fp, $content); //文件写入

4. Close the file fclose($fp);

Note: PHP5 provides a more convenient function file_put_contents, Therefore, the above four steps can be reduced to:

<?php
$content = ‘你好’; 
file_put_contents(’file.txt’,$content);


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