PHP中的fopen't'兼容模式 [英] fopen 't' compatibility mode in php

查看:72
本文介绍了PHP中的fopen't'兼容模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说,fopen可以使用t模式将\n转换为\r\n.所以,问题:

It is said, that fopen can use t mode to convert \n to \r\n. So, questions:

1)当我需要读写(r+)时应该如何使用t模式?是r+t还是rt+tr+?关于b的相同问题,我应该写r+b还是怎么写?

1) How should i use t mode when i need to read and write (r+)? Should it be r+t or rt+ or tr+? Same question for b, should i write r+b or how?

2)我已经尝试使用魔术模式t在debian linux上将所有仅包含\n\r\n的文件转换为文件(想了解其工作原理).但这是行不通的.我究竟做错了什么? t模式什么时候起作用?

2) I've tried all variants on debian linux to convert file, that contains only \n to \r\n using magic mode t (wanna understand how it works). But it does not work. What am I doing wrong? When t mode works?

这是我的代码:

// Write string with \n symbols
$h = fopen('test.file', 'wt');
fwrite($h, "test \ntest \ntest \n"); // I've checked, after file is being created
fclose($h);                          // \n symbols are not substituted to \r\n

// Open file, that contains rows only with \n symbols
$h = fopen('test.file', 'rt');
$data = fread($h, filesize('test.file'));
fclose($h);

// I want to see what's inside
$data = str_replace("\n", '[n]', $data);
$data = str_replace("\r", '[r]', $data);

// finally i have only \n symbols, \r symbols are not added
var_dump($data);

推荐答案

来自: http://php.net/fopen

Windows 提供了文本模式转换标志('t'),在处理文件时,它将透明地将\ n转换为\ r \ n.相反,您也可以使用'b'强制执行二进制模式,该模式不会转换您的数据.要使用这些标志,请将'b'或't'指定为mode参数的最后一个字符.

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. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

因此没有Linux.另外,根据规范r+tr+b都是正确的(但仅适用于Windows).

So no Linux. Also, according to the spec r+t or r+b would be correct (but only on Windows).

这篇关于PHP中的fopen't'兼容模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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