博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php编程中容易忽略的地方
阅读量:5168 次
发布时间:2019-06-13

本文共 4572 字,大约阅读时间需要 15 分钟。

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

注意一:

如果 $mode = 'r' 时候,$filename 不存在  或者 $mode = 'w' 时候,$filename指定的文件不可以创建(如错误的路径或者不存在的路径名就会创建不成功)

则fopen会返回false,并产生一条notice错误日志,一定要记得用 @ 把notice 给屏蔽掉(有次跑个脚本,因为没屏蔽这个notice,而产生了好几百G的错误日志,把硬盘给搞满了)

注意二:

当$mode为’w‘ 时,默认会新建不存在的文件,如果指定的路径不存在,则新建不成功,当$mode = 'r' 不会新建文件

注意三:

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.

注意四:

Note that if specifying the optional 'b' (binary) mode, it appears that it cannot be the first letter for some unaccountable reason. In other words, "br" doesn't work, while "rb" is ok!

 

二:bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

注意一:$message_type = 3 时会自动创建文件,但是不会自动创建文件夹,当文件路径中又文件夹时要注意

 

三:尽管 display_errors 也可以在运行时设置 (使用 ), 但是脚本出现致命错误时任何运行时的设置都是无效的。 因为在这种情况下预期运行的操作不会被执行。

display_startup_errors

例如:

error_reporting(E_ALL);

ini_set('display_errors','Off');
echo 234/0
exit;

这样仍然会报错:Parse error: syntax error, unexpected T_EXIT, expecting ',' or ';'

 

四: pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )

并不检验传递的$path是否合法,是否存在。

<?php

print_r(pathinfo('/no/where/file.txt'));
?>

which will output:
Array
(
    [dirname] => /no/where
    [basename] => file.txt
    [extension] => txt
    [filename] => file
)

 

五:parse_str — 将字符串解析成多个变量   void parse_str ( string $str [, array &$arr ] )

注意:

1:This function automatically urldecodes values 

2:be careful using parse_str() without the [array &$arr] parameter, as this may override values of global and existing variables.

<?php
$var1 = 1;
parse_str('var1=one&var2=two');
// $var1 is now 'one'

 

六:int file_put_contents ( string $filename , $data [, int $flags = 0 [, resource $context ]] )

注意:

1:If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set. (文件不存在可以创建,但是文件所在目录不存在则不能创建)

解决办法:

function file_force_contents($filename, $data, $flags = 0){

    if(!is_dir(dirname($filename)))
       
mkdir(dirname($filename).'/', 0777, TRUE);
    return
file_put_contents($filename, $data,$flags);
}

2:This function returns the number of bytes that were written to the file, or FALSE on failure. (所以如果判断是否正确需要用 === )

3:参数$data可以是 字符串 或 一维数组,具体看:http://php.net/manual/en/function.file-put-contents.php

4:$flags(FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX)

5:This function is binary-safe.

 

例子一:// using the FILE_APPEND flag to append the content to the end of the file

// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file$personFILE_APPEND LOCK_EX);

 

例子二:It should be obvious that this should only be used if you're making one write, if you are writing multiple times to the same file you should handle it yourself with fopen and fwrite, the fclose when you are done writing.

Benchmark below:
file_put_contents() for 1,000,000 writes - average of 3 benchmarks:
real 0m3.932s
user 0m2.487s
sys 0m1.437s
fopen() fwrite() for 1,000,000 writes, fclose() -  average of 3 benchmarks:
real 0m2.265s
user 0m1.819s
sys 0m0.445s

 

例子四:

It's important to understand that LOCK_EX will not prevent reading the file unless you also explicitly acquire a read lock (shared locked) with the PHP 'flock' function.

i.e. in concurrent scenarios file_get_contents may return empty if you don't wrap it like this:
<?php
$myfile
=fopen('test.txt','rt');
flock($myfile,LOCK_SH);
$read=file_get_contents('test.txt');
fclose($myfile);
?>
If you have code that does a file_get_contents on a file, changes the string, then re-saves using file_put_contents, you better be sure to do this correctly or your file will randomly wipe itself out.

 

七: string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

The function returns the read data or FALSE on failure.

注意:

1:An E_WARNING level error is generated if filename cannot be found, maxlength is less than zero, or if seeking to the specified offset in the stream fails.

2:This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on for more information. Use for testing the return value of this function.

转载于:https://www.cnblogs.com/Alight/p/3934792.html

你可能感兴趣的文章
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>
35. Search Insert Position(C++)
查看>>
[毕业生的商业软件开发之路]C#异常处理
查看>>
有关快速幂取模
查看>>
NOI2018垫底记
查看>>
注意java的对象引用
查看>>
C++ 面向对象 类成员函数this指针
查看>>
NSPredicate的使用,超级强大
查看>>
自动分割mp3等音频视频文件的脚本
查看>>
判断字符串是否为空的注意事项
查看>>
布兰诗歌
查看>>
(转)Tomcat 8 安装和配置、优化
查看>>
(转)Linxu磁盘体系知识介绍及磁盘介绍
查看>>
命令ord
查看>>