PHP函数fwrite -- 写入文件(可安全用于二进制文件)
说明
int fwrite ( resource handle, string string [, int length] )
PHP函数fwrite把 string 的内容写入 文件指针 handle 处。 如果指定了 length,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况。
v返回写入的字符数,出现错误时则返回 FALSE 。
注意如果给出了 length 参数,则 magic_quotes_runtime 配置选项将被忽略,而 string 中的斜线将不会被抽去。
注意: 在区分二进制文件和文本文件的系统上(如 Windows) 打开文件时,fopen() 函数的 mode 参数要加上 'b'。
例 1. 一个简单的PHP函数fwrite例子
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">filename</span><span> = </span><span class="attribute-value">'test.txt'</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">somecontent</span><span> = </span><span class="attribute-value">"添加这些文字到文件n"</span><span>; </span> </li> <li><span>// 首先我们要确定文件存在并且可写。 </span></li> <li class="alt"><span>if (is_writable($filename)) { </span></li> <li><span>// 在这个例子里,我们将使用添加模式<br>打开$filename, </span></li> <li class="alt"><span>// 因此,文件指针将会在文件的开头, </span></li> <li><span>// 那就是当我们使用fwrite()的时候,<br>$somecontent将要写入的地方。 </span></li> <li class="alt"> <span>if (!$</span><span class="attribute">handle</span><span> = </span><span class="attribute-value">fopen</span><span>($filename, 'a')) { </span> </li> <li><span>echo "不能打开文件 $filename"; </span></li> <li class="alt"><span>exit; </span></li> <li><span>} </span></li> <li class="alt"><span>// 将$somecontent写入到我们打开的文件中。 </span></li> <li><span>if (fwrite($handle, $somecontent) <br>=== FALSE) { </span></li> <li class="alt"><span>echo "不能写入到文件 $filename"; </span></li> <li><span>exit; </span></li> <li class="alt"><span>} </span></li> <li><span>echo "成功地将 $somecontent 写入<br>到文件$filename"; </span></li> <li class="alt"><span>fclose($handle); </span></li> <li><span>} else { </span></li> <li class="alt"><span>echo "文件 $filename 不可写"; </span></li> <li><span>} </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
以上就是PHP函数fwrite的相关使用方法。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
