PHP中的fwrite()函数是一个内置函数,用于写入打开的文件。fwrite()函数在文件末尾或达到作为参数传递的指定长度时停止,以先到者为准。必须写入的文件、字符串和长度作为参数发送给fwrite()函数,它在成功时返回写入的字节数,在失败时返回FALSE。
null
语法:
fwrite(file, string, length)
参数: PHP中的fwrite()函数接受三个参数。
- 文件 :它是指定文件的必需参数。
- 一串 :它是一个强制参数,指定要写入的字符串。
- 长 :这是一个可选参数,指定要写入的最大字节数。
返回值: 它返回成功时写入的字节数,或失败时返回False。
例外情况 :
- 由于fwrite()是二进制安全的,因此可以使用此函数写入二进制数据,如图像和字符数据。
- 如果对文件指针执行两次写入操作,则数据将附加到文件内容的末尾。
例如:
Input : $myfile = fopen("gfg.txt", "w"); echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!"); fclose($myfile); Output : 36 Input : $myfile = fopen("gfg.txt", "w"); echo fwrite($myfile, "PhP is Simple to Learn!"); echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!"); fclose($myfile); Output : 23 59
下面的程序演示了fwrite()函数:
方案1 :
<?php // Opening a file $myfile = fopen ( "gfg.txt" , "w" ); // writing content to a file using fwrite() echo fwrite( $myfile , "Geeksforgeeks is a portal for geeks!" ); // closing the file fclose( $myfile ); ?> |
输出:
36
方案2 :
<?php // Opening a file $myfile = fopen ( "gfg.txt" , "w" ); // writing content to a file using fwrite() echo fwrite( $myfile , "PhP is Simple to Learn!" ); echo fwrite( $myfile , "Geeksforgeeks is a portal for geeks!" ); // closing the file fclose( $myfile ); ?> |
输出:
23 59
参考: http://php.net/manual/en/function.fwrite.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END