fopen 什么是 b 标志
fopen what is the b flag
在阅读 php 的 documentation for php fopen 时,我看到以下内容:
For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().
什么是 b
标志,它有什么作用?
为什么强烈推荐?
'b' 标志强制二进制模式。
如果你想处理二进制文件,你可以使用 'b' 标志,即。图片。
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.
您可以通过在 mode
参数中使用 'b' 标志来避免翻译。用法示例:
$handle_read = fopen($filepath, 'rb');
$handle_write = fopen("/home/user/file.gif", "wb");
所以...推荐的原因在 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.
'b' 标志的用法也记录在 fwrite()
和 fread()
的手册页上,它们是二进制安全文件 read/write 函数。
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);
在阅读 php 的 documentation for php fopen 时,我看到以下内容:
For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().
什么是 b
标志,它有什么作用?
为什么强烈推荐?
'b' 标志强制二进制模式。
如果你想处理二进制文件,你可以使用 'b' 标志,即。图片。
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.
您可以通过在 mode
参数中使用 'b' 标志来避免翻译。用法示例:
$handle_read = fopen($filepath, 'rb');
$handle_write = fopen("/home/user/file.gif", "wb");
所以...推荐的原因在 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.
'b' 标志的用法也记录在 fwrite()
和 fread()
的手册页上,它们是二进制安全文件 read/write 函数。
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);