百科狗-知识改变命运!
--

fwrite() - 写入文件(可安全用于二进制文件) - php 文件目录函数

是丫丫呀1年前 (2023-11-21)阅读数 20#技术干货
文章标签文件

fwrite()

(PHP 4, PHP 5, PHP 7)

写入文件(可安全用于二进制文件)

说明

fwrite(resource $handle,string $string[,int $length]): int

fwrite()把$string的内容写入文件指针$handle处。

参数

$handle

fwrite() - 写入文件(可安全用于二进制文件) - php 文件目录函数

文件系统指针,是典型地由fopen()创建的resource(资源)。

$string

The 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() 打开文件或者 URL
  • fsockopen() 打开一个网络连接或者一个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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)