search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

首页课程PHP Fun Breakthrough Classfwrite cooperates with fopen for writing operations

fwrite cooperates with fopen for writing operations

目录列表

fwrite配合fopen进行写入操作

fwrite写入文件

fwrite基本语法:

// int fwrite ( resource $文件资源变量, string $写入的字符串 [, int 长度])

注:fwrite的别名函数是fputs

我们上节课试了r模式,只道是读取的时候使用,接下来我们用fwrite加上fopen中的w,写入模式来进行文件写入。

我们来看一下特点:

写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建。

注意:在下面的实验中,你可以试试新建个test.txt文件向里面写入内容。然后,可以试试把test.txt删除。看看有什么提示。

<?php
   $filename = 'test.txt';
   $fp= fopen($filename, "w");
   $len = fwrite($fp, '我是一只来自北方的狼,却在南方冻成了狗');
   fclose($fp);
   print $len .'字节被写入了\n';
?>

总结:

  • 不论有没有新建都会打开文件重新写入

  • 原有的文件内容会被覆盖掉

  • 文件不存在会创建

上一章提到的模式我们不会一一演示的,大家可以自己多多练习一下。

将下列排序修改正确。

  • fclose($fp);
  • $fp= fopen('php_cn.txt', "w");
  • $len = fwrite($fp, '全国最大的线上IT技术学习网站!');
1/2