php 写文件操作的步骤分享

WBOY
Release: 2016-07-25 08:56:48
Original
1344 people have browsed it
本文介绍下,在php编程中对文件进行写操作的步骤,有需要的朋友,可以参学习下。

在php中,写入文件一般要经过如下的步骤: 1、首先,确定要写入文件的内容 $content = ‘你好’;

2、然后,打开文件(系统会自动建立这个空文件)

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

3、将内容字符串写入文件 //$fp告诉系统要写入的文件,写入的内容是$content。

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

4、关闭文件 fclose($fp);

说明:PHP5中提供了更方便的函数file_put_contents, 因此以上四步可以缩减为:

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!