PHP:无法打开流:文件存在

Moa*_*oak 1 php fopen

从类似的问题我使用此代码来调试打开文件的失败

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>
Run Code Online (Sandbox Code Playgroud)

我收到这个有用的消息

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)
}
Run Code Online (Sandbox Code Playgroud)

"未能打开流:文件存在" - 这意味着什么?

mar*_*rio 6

发生这种情况是因为您使用了'x'标志fopen().PHP手册说明了这一点:

'x' 创建并打开仅供写入; 将文件指针放在文件的开头.如果文件已存在,则fopen()调用将失败,返回FALSE并生成级别为E_WARNING的错误.如果该文件不存在,请尝试创建它.这相当于为底层的open(2)系统调用指定O_EXCL | O_CREAT标志.

您可能想要使用fopen($file, 'c')或只是普通w模式.