这段PHP代码通过fopen以写入模式打开文件,然后向其中写入文本字符串,最后关闭文件,代码简单实用
/**
* PHP写入文本文件范例
*
* @param
* @arrange 512-笔记网: www.jb51.cc
**/
<?PHP
$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename,"w" );
if( $file == false )
{
echo ( "Error in opening new file" );
exit();
}
fwrite( $file,"This is a simple test\n" );
fclose( $file );
?>
<html>
<head>
<title>Writing a file using PHP</title>
</head>
<body>
<?PHP
if( file_exist( $filename ) )
{
$filesize = filesize( $filename );
$msg = "File created with name $filename ";
$msg .= "containing $filesize bytes";
echo ($msg );
}
else
{
echo ("File $filename does not exit" );
}
?>
</body>
</html>
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
原文链接:https://www.f2er.com/php/528294.html