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

Sharing steps for writing files in php

WBOY
Release: 2016-07-25 08:56:48
Original
1492 people have browsed it
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’);
Copy after login

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); //文件写入
Copy after login

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);
Copy after login


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