Home > php教程 > php手册 > body text

php 读取文件内容与向文件写入数据

WBOY
Release: 2016-06-13 11:17:38
Original
868 people have browsed it

php 读取文件内容与向文件写入数据这里主要是讲一行行写数据到文件,也一行行读出文件的内容。  

php教程 读取文件内容与向文件写入数据
这里主要是讲一行行写数据到文件,也一行行读出文件的内容。
*/

$fp = fopen($_server['document_root']."/../data/info.dat",'r');

if(!$fp)
{
    echo "error: 打开文件错误,请检查目录是否正确,或稍后再试!";
    exit;
}

while(!feof($fp))
{
    $line = fgets($fp);
    echo $line;
    echo '
';
}

fclose($fp);

//写文件

$file = "data.txt";
$content = "内容标题rnwww.bkjia.comrn内容第二行";    //要写入的内容

if(!$fp = fopen($file,'a'))                           //打开文件$file时,使用追加模式,此时文件指针会在文件开始处
{
    echo "打开文件$file失败!";
    exit;
}

if(fwrite($fp,$content) === false)                    //将内容写入文件
{
    echo "写入文件失败!";
    exit;
}

echo "写入文件成功!";
fclose($fp);


Related labels:
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 Recommendations
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!