fwrite() - 写入文件(可安全用于二进制文件) - php 文件目录函数
fwrite()
(PHP 4, PHP 5, PHP 7)
写入文件(可安全用于二进制文件)
说明
fwrite(resource $handle,string $string[,int $length]): intfwrite()把$string的内容写入文件指针$handle处。
参数
$handle文件系统指针,是典型地由fopen()创建的resource(资源)。
$stringThe string that is to be written.
$length如果指定了$length,当写入了$length个字节或者写完了$string以后,写入就会停止,视乎先碰到哪种情况。
注意如果给出了$length参数,则magic_quotes_runtime配置选项将被忽略,而$string中的斜线将不会被抽去。
返回值
fwrite()返回写入的字符数,出现错误时则返回FALSE
。
注释
Note:Writing to a network stream may end before the whole string is written. Return value offwrite()may be checked:
Note:
在区分二进制文件和文本文件的系统上(如 Windows)打开文件时,fopen()函数的 mode 参数要加上'b'。Note:
If$handlewasfopen()ed in append mode,fwrite()s are atomic (unless the size of$stringexceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need toflock()a resource before callingfwrite(); all of the data will be written without interruption.
Note:
If writing twice to the file pointer, then the data will be appended to the end of the file content:
范例
一个简单的fwrite()例子
参见
fread()
读取文件(可安全用于二进制文件)fopen()
打开文件或者 URLfsockopen()
打开一个网络连接或者一个Unix套接字连接popen()
打开进程文件指针file_get_contents()
将整个文件读入一个字符串
After having problems with fwrite() returning 0 in cases where one would fully expect a return value of false, I took a look at the source code for php's fwrite() itself. The function will only return false if you pass in invalid arguments. Any other error, just as a broken pipe or closed connection, will result in a return value of less than strlen($string), in most cases 0. Therefore, looping with repeated calls to fwrite() until the sum of number of bytes written equals the strlen() of the full value or expecting false on error will result in an infinite loop if the connection is lost. This means the example fwrite_stream() code from the docs, as well as all the "helper" functions posted by others in the comments are all broken. You *must* check for a return value of 0 and either abort immediately or track a maximum number of retries. Below is the example from the docs. This code is BAD, as a broken pipe will result in fwrite() infinitely looping with a return value of 0. Since the loop only breaks if fwrite() returns false or successfully writes all bytes, an infinite loop will occur on failure.
Some people say that when writing to a socket not all of the bytes requested to be written may be written. You may have to call fwrite again to write bytes that were not written the first time. (At least this is how the write() system call in UNIX works.) This is helpful code (warning: not tested with multi-byte character sets) function fwrite_with_retry($sock, &$data) { $bytes_to_write = strlen($data); $bytes_written = 0; while ( $bytes_written// you want copy dummy file or send dummy file // it is possible to send a file larger than 4GB and write without FSEEK used is limited by PHP_INT_MAX. it works on a system 32-bit or 64-bit // fwrite and fread non pas de limite de position du pointeur
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!