码迷,mamicode.com
首页 > Web开发 > 详细

php分享(三十五) 文件多写注意事项

时间:2016-04-01 13:11:03      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:

1: resource fopen ( string $filename , string $mode [, bool $use_include_path = false [,resource $context ]] )

fopen() 将 filename 指定的名字资源绑定到一个流上

1: fopen写的时候注意习惯加上第三个参数
2: 如果 PHP 认为 filename 指定的是一个本地文件,将尝试在该文件上打开一个流。该文件必须是 PHP 可以访问的,因此需要确认文件访问权限允许该访问。如果激活了安全模式或者 open_basedir
 则会应用进一步的限制。
3: 如果打开失败,会产生一个 E_WARNING 错误,所以用之前,先用file_exist检查文件是否真的存在和有权限操作,习惯在之前加@阻止报错
4: 模式
可以自动创建文件的有:
‘w‘    写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
‘w+‘    读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
‘a‘    写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
‘a+‘    读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。

如果不存在,则创建,存在则报错返回false 模式:
‘x‘    创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。
这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。
‘x+‘    创建并以读写方式打开,其他的行为和 ‘x‘ 一样。

如果文件不存在,则创建,如果文件已经存在,则c模式既不会把文件大小截为0(像w模式),也不会报错(像x模式),文件指针被定为到文件头,
‘c‘    Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to ‘w‘),
nor the call to this function fails (as is the case with ‘x‘). The file pointer is positioned on the beginning of the file. This may be useful
if it‘s desired to get an advisory lock (see flock()) before attempting to modify the file, as using ‘w‘ could truncate the file before the
lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).

‘c+‘    Open the file for reading and writing; otherwise it has the same behavior as ‘c‘.

5: 为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 ‘b‘ 标记。

Windows 下提供了一个文本转换标记(‘t‘)可以透明地将 \n 转换为 \r\n。与此对应还可以使用 ‘b‘ 来强制使用二进制模式,这样就不会转换数据。要使用这些标记,要么用 ‘b‘ 或者用 ‘t‘ 作为 mode 参数的最后一个字符。

默认的转换模式依赖于 SAPI 和所使用的 PHP 版本,因此为了便于移植鼓励总是指定恰当的标记。如果是操作纯文本文件并在脚本中使用了 \n 作为行结束符,但还要期望这些文件可以被其它应用程序例如 Notepad 读取,则在 mode 中使用 ‘t‘。在所有其它情况下使用 ‘b‘

在操作二进制文件时如果没有指定 ‘b‘ 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于\r\n 字符的奇怪问题。

6: 该函数也可以用于打开目录;
This function may also succeed when filename is a directory. If you are unsure whether filename is a file or a directory, you may need to use
the is_dir() function before calling fopen().


7:用w模式打开的文件不会更新更改时间(modification time,filemtime),你必须在写入后用touch来更改时间,以保证某些缓存系统会用修改时间这个来判断
Note - using fopen in ‘w‘ mode will NOT update the modification time (filemtime) of a file like you may expect. You may want to issue a touch()
after writing and closing the file which update its modification time. This may become critical in a caching situation, if you intend to keep your hair.

 

2: bool feof ( resource $handle )

1:handle文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。

2:如果文件指针到了 EOF 或者出错时则返回 TRUE,否则返回一个错误(包括 socket 超时),其它情况则返回 FALSE。

3:应该在用feof钱判断handle是否有效,因为如果传递的文件指针无效可能会陷入无限循环中,因为 feof() 不会返回 TRUE
判断方法:
if(gettype($source) == "resource") 
或
if (!$soure)

 

php分享(三十五) 文件多写注意事项

标签:

原文地址:http://www.cnblogs.com/Alight/p/5344544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!