rename() 给出未记录的 return 值 NULL
rename() gives undocumented return value NULL
从 PHP7.0 更新到 PHP7.2 后,一些代码停止工作,我不明白为什么:
$res = rename($tmpfile, $output_file);
if ($res !== true) {
throw new Exception("Unable to rename temporary file");
}
在php7.2 中触发了异常,虽然重命名已成功完成,但文件完全可读并且没有任何错误。我仍然得到异常,因为 return 值为 NULL。我插入了一些调试代码:
$res = rename($tmpfile, $output_file);
log('Debug: $res ' . $res);
log('Debug: $res true? ' . $res === true);
log('Debug: $res type ' . gettype($res));
if ($res !== true) {
throw new Exception("Unable to rename temporary file");
}
这给出了以下输出:
2018-08-03 10:37:39 Debug: $res
2018-08-03 10:37:39
2018-08-03 10:37:39 Debug: $res type NULL
2018-08-03 10:37:39 CURL_DOWNLOAD Exception: Unable to rename temporary file
#0 /var/www/formr.org/application/Library/Functions.php(1470): CURL::DownloadUrl('http://docs.goo...', '/var/www/formr....', NULL, 'GET', Array, Array)
可以看到,没有输出,类型为NULL。这是一种无证行为。 According to the php documentation return 值只能是 true 或 false。
我找不到任何说明此函数的行为已更改的文档。有人明白吗?
感谢您的帮助!
编辑:var_dump 输出
$res = rename($tmpfile, $output_file);
ob_start();
var_dump($res);
$contents = ob_get_contents();
ob_end_clean();
log('Debug $res: ' . $contents);
给出:
2018-08-03 12:37:40 Debug $res: NULL
正在终端中测试重命名 () 功能:
user$ sudo su www-data -s /bin/bash
www-data$ php -a
Interactive mode enabled
php > $old='/var/www/path/changed/filename.xlsx';
php > $new='/var/www/path/changed/newfilename.xlsx';
php > $res = rename($old, $new);
php > var_dump($res);
bool(true)
(出于隐私原因,我更改了文件名和路径,但我在原始文件上进行了测试。)
PHP版本:
$ php --version
PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
安装的 PHP 版本是 Ubuntu 18.04 附带的版本。我没有自己重新编译它。
正如我刚刚注意到的,该程序有自己的两次重命名功能。但它们不是关于重命名文件,而是重命名数据库中的某些内容。
它们都只有一个参数。 PHP 不支持重载或按参数数量区分,所以可能会有一些干扰?到目前为止,这还不是问题。而且由于文件实际上已重命名,我不认为调用了错误的函数。
由 Apokryfos 的最后一条评论解决:使用命名空间 \rename($old,$new)
正确调用重命名函数给我 bool(true)
作为 return 值。
从 PHP7.0 更新到 PHP7.2 后,一些代码停止工作,我不明白为什么:
$res = rename($tmpfile, $output_file);
if ($res !== true) {
throw new Exception("Unable to rename temporary file");
}
在php7.2 中触发了异常,虽然重命名已成功完成,但文件完全可读并且没有任何错误。我仍然得到异常,因为 return 值为 NULL。我插入了一些调试代码:
$res = rename($tmpfile, $output_file);
log('Debug: $res ' . $res);
log('Debug: $res true? ' . $res === true);
log('Debug: $res type ' . gettype($res));
if ($res !== true) {
throw new Exception("Unable to rename temporary file");
}
这给出了以下输出:
2018-08-03 10:37:39 Debug: $res
2018-08-03 10:37:39
2018-08-03 10:37:39 Debug: $res type NULL
2018-08-03 10:37:39 CURL_DOWNLOAD Exception: Unable to rename temporary file
#0 /var/www/formr.org/application/Library/Functions.php(1470): CURL::DownloadUrl('http://docs.goo...', '/var/www/formr....', NULL, 'GET', Array, Array)
可以看到,没有输出,类型为NULL。这是一种无证行为。 According to the php documentation return 值只能是 true 或 false。
我找不到任何说明此函数的行为已更改的文档。有人明白吗?
感谢您的帮助!
编辑:var_dump 输出
$res = rename($tmpfile, $output_file);
ob_start();
var_dump($res);
$contents = ob_get_contents();
ob_end_clean();
log('Debug $res: ' . $contents);
给出:
2018-08-03 12:37:40 Debug $res: NULL
正在终端中测试重命名 () 功能:
user$ sudo su www-data -s /bin/bash
www-data$ php -a
Interactive mode enabled
php > $old='/var/www/path/changed/filename.xlsx';
php > $new='/var/www/path/changed/newfilename.xlsx';
php > $res = rename($old, $new);
php > var_dump($res);
bool(true)
(出于隐私原因,我更改了文件名和路径,但我在原始文件上进行了测试。)
PHP版本:
$ php --version
PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
安装的 PHP 版本是 Ubuntu 18.04 附带的版本。我没有自己重新编译它。
正如我刚刚注意到的,该程序有自己的两次重命名功能。但它们不是关于重命名文件,而是重命名数据库中的某些内容。
它们都只有一个参数。 PHP 不支持重载或按参数数量区分,所以可能会有一些干扰?到目前为止,这还不是问题。而且由于文件实际上已重命名,我不认为调用了错误的函数。
由 Apokryfos 的最后一条评论解决:使用命名空间 \rename($old,$new)
正确调用重命名函数给我 bool(true)
作为 return 值。