学而实习之 不亦乐乎

PHP中文件的读写操作

2019-01-20 16:01:14

1.PHP中读写的基本过程

从文件读取数据的过程:
1.打开文件,如果出现错误或异常就正常退出。
2.从文件中读取数据。
3.关闭文件。

将数据写入文件的过程:
1.打开文件。如果不存在,需要先创建。
2.将数据写入这个文件。
3.关闭文件。

2.下面给出参考实例。

<?php
    $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
    echo fread($myfile,filesize("webdictionary.txt"));
    fclose($myfile);
?>
<?php
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "Bill Gates
";
    fwrite($myfile, $txt);
    $txt = "Steve Jobs
";
    fwrite($myfile, $txt);
    fclose($myfile);
?>