PHP:未能打开流:文件存在
问题描述:
从我用这个代码调试打开文件PHP:未能打开流:文件存在
echo'<pre>';
error_reporting(E_ALL);
ini_set('display_errors', true);
echo 'phpversion: ', phpversion(), "\n";
echo 'uname: ', php_uname("s r"), "\n"; // name/release of the operating system
//echo 'sapi: ', php_sapi(), "\n";
echo $file, file_exists($file) ? ' exists' : ' does not exist', "\n";
echo $file, is_readable($file) ? ' is readable' : ' is NOT readable', "\n";
echo $file, is_writable($file) ? ' is writable' : ' is NOT readable', "\n";
$fp = @fopen($file, 'x');
if (! $fp) {
echo 'last error: ';
var_dump(error_get_last());
}
echo '</pre>
的失败,我得到这个有用的消息
D:\data\openid\nonces\4d895d80---2jmj7l5rSw0yVb.vlWAYkK.YBwk-Bk0DdMtjVYDVZi0npvGwNNFSRy0
phpversion: 5.3.1
uname: Windows NT
D:\data\openid\nonces\4d895...FSRy0 exists
D:\data\openid\nonces\4d895...FSRy0 is readable
D:\data\openid\nonces\4d895...FSRy0 is writable
last error: array(4) {
["type"]=>
int(2)
["message"]=>
string(188) "fopen(D:\data\openid\nonces\4d895d80---2jmj7l5rSw0yVb.vlWAYkK.YBwk-Bk0DdMtjVYDVZi0npvGwNNFSRy0) [function.fopen]: failed to open stream: File exists"
["file"]=>
string(38) "D:\web\library\Utils.php"
["line"]=>
int(179)
}
类似的问题“未能开放流:文件存在“ - 这意味着什么?
答
发生这种情况是因为您使用'x'
标志为fopen()
。 PHP手册说的只是:
'X'
创建并以写入方式打开;将文件指针放在文件的开头。 如果该文件已经存在,fopen()调用将失败,返回FALSE并生成级别为E_WARNING的错误。如果文件不存在,请尝试创建它。这相当于为底层open(2)系统调用指定O_EXCL | O_CREAT标志。
您可能想要使用fopen($file, 'c')
或只是普通的w
模式。
实际上,'x'标志位是我集成的开放ID库的一部分https://github.com/openid/php-openid我需要在那里请求支持,我猜想,他们故意放置该标志 – Moak 2011-03-23 03:23:57
其目的当然是防止覆盖现有的文件。我假设他们正在使用它自己创建会话文件。 – mario 2011-03-23 03:26:15