打开什么是b标志 [英] fopen what is the b flag

查看:355
本文介绍了打开什么是b标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读php的文档中,我看到以下内容。对于可移植性,强烈建议您在使用[fopen] [1]()打开文件时始终使用b标志。



b标志是什么,它做什么?

为什么强烈推荐?

解决方案

'b'标志强制二进制模式。

如果你想处理二进制文件,你可以使用'b'标志。一个图像。


注意: 你写一个文本文件并且想插入一个换行符,你需要为你的操作系统使用正确的换行符。基于Unix的系统使用 \ n 作为行尾字符,基于Windows的系统使用 \r\\\
作为结尾字符的行,而基于Macintosh的系统使用 \r 作为行尾字符。 Windows提供了一个文本模式转换标志('t'),它可以将 \\\
透明地转换为
code> \r\\\
处理文件。


相反,您也可以使用'b'来强制执行二进制模式,而不会翻译您的数据。 $ b

您可以通过在模式参数中使用'b'标志来避免翻译。用法示例:

  $ handle_read = fopen($ filepath,'rb'); 

$ handle_write = fopen(/ home / user / file.gif,wb);

所以...这个建议的理由清楚的说明在


如果在使用二进制文件时未指定b标志,则可能会遇到有关数据的奇怪问题,包括破碎的图像文件和\ r \\ n字符出现的奇怪问题。



fwrite()
lock
$ block $ $ $ $ $ $ $ $ $ $ $ $ $ $在区分二进制和文本文件(例如Windows)的系统上,必须打开包含b的文件。




  $ filename =c:\\files \\somepic.gif; 
$ handle = fopen($ filename,rb);
$ contents = fread($ handle,filesize($ filename));
fclose($ handle);


In reading the documentation for php fopen for php I see the following.

For portability, it is strongly recommended that you always use the 'b' flag when opening files with [fopen][1]().

What is the b flag and what does it do ?
Why is it strongly recommended ?

解决方案

The 'b' flag forces binary mode.

You use the 'b' flag if you want to deal with binary files, ie. an image.

Note:

When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system.

Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.

Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file.

In contrast, you can also use 'b' to force binary mode, which will not translate your data.

You can avoid the translation by using the 'b' flag in the mode parameter. Example usage:

$handle_read  = fopen($filepath, 'rb');

$handle_write = fopen("/home/user/file.gif", "wb");

So... the reason this is recommended is clearly stated on the manual:

If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.

The usage of 'b' flag is also noted on manual pages of fwrite() and fread() which are binary-safe file read/write functions.

Warning:

On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.

$filename = "c:\\files\\somepic.gif";
$handle   = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);

这篇关于打开什么是b标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆