I'm surprised this isn't mentioned in docs here, but to set these values at runtime use "ini_set()". For example:
<?php
ini_set("auto_detect_line_endings", true);
// Now I can invoke fgets() on files that contain silly \r line endings.
?>
PHP - Manual: 运行时配置
2024-12-22
这些函数的行为受 php.ini 中的设置影响。
名字 | 默认 | 可修改范围 | 更新日志 |
---|---|---|---|
allow_url_fopen | "1" | PHP_INI_SYSTEM | |
allow_url_include | "0" | PHP_INI_SYSTEM | 自 PHP 7.4.0 起废弃。 |
user_agent | NULL | PHP_INI_ALL | |
default_socket_timeout | "60" | PHP_INI_ALL | |
from | "" | PHP_INI_ALL | |
auto_detect_line_endings | "0" | PHP_INI_ALL | |
sys_temp_dir | "" | PHP_INI_SYSTEM |
这是配置指令的简短说明。
allow_url_fopen
bool
本选项激活了 URL 形式的 fopen 封装协议使得可以访问 URL 对象例如文件。默认的封装协议提供用 ftp 和 http 协议来访问远程文件,一些扩展库例如 zlib 可能会注册更多的封装协议。
allow_url_include
bool
This option allows the use of URL-aware fopen wrappers with the following functions: include, include_once, require, require_once.
注意:
这个设置项需要开启 allow_url_fopen 。
user_agent
string
定义 PHP 发送的 User-Agent。
default_socket_timeout
int
指定基于 socket 的流的默认超时时间(秒)。指定一个负值意味着不会超时。
from
string
这个 email 地址在使用 ftp 封装器的时候用于未认证的 FTP 连接,在使用 http 封装器的时候用于 HTTP 连接的消息头中的 From 字段。
auto_detect_line_endings
bool
当设为 On 时,PHP 将检查通过 fgets() 和 file() 取得的数据中的行结束符号是符合 Unix,MS-DOS,还是 Macintosh 的习惯。
这使得 PHP 可以和 Macintosh 系统交互操作,但是默认值是 Off,因为在检测第一行的 EOL 习惯时会有很小的性能损失,而且在 Unix 系统下使用回车符号作为项目分隔符的人们会遭遇向下不兼容的行为。
sys_temp_dir
string
I'm surprised this isn't mentioned in docs here, but to set these values at runtime use "ini_set()". For example:
<?php
ini_set("auto_detect_line_endings", true);
// Now I can invoke fgets() on files that contain silly \r line endings.
?>
If you want to use auto_detect_line_endings, e.g. to recognize carriage return on a Classic Mac file, you must set it before calling fopen. You can then reset it to its original value. E.g,
$original = ini_get("auto_detect_line_endings");
ini_set("auto_detect_line_endings", true);
$handle = fopen($someFile, "r");
ini_set("auto_detect_line_endings", $original);
while (($line = fgets($handle)) !== false) {
echo "$line\n"; // etc
}
(Reference: https://bugs.php.net/bug.php?id=63341&edit=2)
Keep in mind also that Mac OS X bash does not handle carriage returns well, so if it seems like your code is not working when testing from the command line, redirect your output to a file and then try looking at that. On my system, doing it directly on the command line only showed the last line (with or without this setting turned on).
Also note that this will not do what you want if you have a file with mixed line endings (!). If you really care about that case, you have to do something else, like run the file through a translation first and then read it.
Please note that although you may try to set default_socket_timeout to something over 20s, you may get tricked by the Linux kernel.
The default value of tcp_syn_retries is set to 5, which will effectively timeout any TCP connection after roughly 20s, no matter what limits you set in PHP higher than this.
The value can be altered by root only, like this:
echo 6 > /proc/sys/net/ipv4/tcp_syn_retries
A value of 6, as above, will give you a timeout up to ~45s.
官方地址:https://www.php.net/manual/en/filesystem.configuration.php