Php fwrite写NUL

Php fwrite写NUL,php,file,fwrite,nul,Php,File,Fwrite,Nul,我正试图用PHP编写一个文件,这是我正在使用的代码(摘自上一个问题): 读取部分工作正常,如果文件被读取,则其内容读取良好,即,如果文件包含2289,则读取2289 问题是,当它递增并重写该文件的值时,[NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL]1被写入 我错过了什么?为什么要写入空字符?EDIT#2: 用flock试试这个(已测试) 若文件未锁定,它将抛出一个异常(请参阅添加的行),若(。。。 我从借用了异常片段 <?php $filena

我正试图用PHP编写一个文件,这是我正在使用的代码(摘自上一个问题):

读取部分工作正常,如果文件被读取,则其内容读取良好,即,如果文件包含
2289
,则读取
2289

问题是,当它递增并重写该文件的值时,
[NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL]1
被写入

我错过了什么?为什么要写入空字符?

EDIT#2: 用flock试试这个(已测试)

若文件未锁定,它将抛出一个异常(请参阅添加的行)
,若
(。。。 我从借用了异常片段

<?php

$filename = "numbers.txt";
$filename = fopen($filename, 'a') or die("can't open file");

if (!flock($filename, LOCK_EX)) {
    throw new Exception(sprintf('Unable to obtain lock on file: %s', $filename));
}

file_put_contents('numbers.txt', ((int)file_get_contents('numbers.txt'))+1);

// To show the contents of the file, you 
// include("numbers.txt");

    fflush($filename);            // flush output before releasing the lock
    flock($filename, LOCK_UN);    // release the lock


fclose($filename);
echo file_get_contents('numbers.txt');

?>
EDIT#2:
用flock试试这个(已测试)

若文件未锁定,它将抛出一个异常(请参阅添加的行)
,若
(。。。 我从借用了异常片段

<?php

$filename = "numbers.txt";
$filename = fopen($filename, 'a') or die("can't open file");

if (!flock($filename, LOCK_EX)) {
    throw new Exception(sprintf('Unable to obtain lock on file: %s', $filename));
}

file_put_contents('numbers.txt', ((int)file_get_contents('numbers.txt'))+1);

// To show the contents of the file, you 
// include("numbers.txt");

    fflush($filename);            // flush output before releasing the lock
    flock($filename, LOCK_UN);    // release the lock


fclose($filename);
echo file_get_contents('numbers.txt');

?>

您可以使用此代码,一个简化版本,但不确定它是否是最好的:

<?php
$fr = fopen("count.txt", "r");
$text = fread($fr, filesize("count.txt"));
$fw = fopen("count.txt", "w");
$text++;
fwrite($fw, $text);
?>

您可以使用此代码,一个简化版本,但不确定它是否是最好的:

<?php
$fr = fopen("count.txt", "r");
$text = fread($fr, filesize("count.txt"));
$fw = fopen("count.txt", "w");
$text++;
fwrite($fw, $text);
?>

您缺少的是倒带()。没有它,在截断为0字节后,指针仍然不在开头()。因此,当您写入新值时,它会在文件中填充
NULL

此脚本将读取当前计数的文件(如果不存在,则创建),每次加载页面时递增,并将其写回同一文件

$filename = date('Y-m-d').".txt";

$fp = fopen($filename, "c+"); 
if (flock($fp, LOCK_EX)) {
    $number = intval(fread($fp, filesize($filename)));
    $number++;

    ftruncate($fp, 0);    // Clear the file
    rewind($fp);          // Move pointer to the beginning
    fwrite($fp, $number); // Write incremented number
    fflush($fp);          // Write any buffered output
    flock($fp, LOCK_UN);  // Unlock the file
}
fclose($fp);

您缺少的是倒带()。没有它,在截断为0字节后,指针仍然不在开头()。因此,当您写入新值时,它会在文件中填充
NULL

此脚本将读取当前计数的文件(如果不存在,则创建),每次加载页面时递增,并将其写回同一文件

$filename = date('Y-m-d').".txt";

$fp = fopen($filename, "c+"); 
if (flock($fp, LOCK_EX)) {
    $number = intval(fread($fp, filesize($filename)));
    $number++;

    ftruncate($fp, 0);    // Clear the file
    rewind($fp);          // Move pointer to the beginning
    fwrite($fp, $number); // Write incremented number
    fflush($fp);          // Write any buffered output
    flock($fp, LOCK_UN);  // Unlock the file
}
fclose($fp);

这让我发疯,我以为是字符编码,BOM等。结果是这样!非常感谢这应该是公认的答案。它解释了问题中
NUL
条目的原因。这让我发疯,我以为是字符编码,BOM等。结果是这个!非常感谢。这应该是公认的anwser。它解释了问题中
NUL
条目的原因。这是第二个问题,但这里的锁定代码非常奇怪。如果没有
LOCK\u NB
标志,
flock
将阻塞,直到它可以获得锁为止,因此循环等待没有什么意义g让它返回
true
。它返回
false
而不带
LOCK\u NB
的唯一原因是,如果您未能打开文件并将其作为第一个参数传递
null
,等待肯定不会有任何帮助……而循环意味着您将在该场景的无限循环中发出警告。@MarkAmery我从来都不是PHP专家,特别是在2013年,我才刚刚开始我的职业生涯,所以这肯定是一个初学者的错误:)谢谢你指出这一点!这是你要问的第二个问题,但是这里的锁定代码非常奇怪。如果没有
LOCK\u NB
标志,
flock
将被阻止,直到它能够获得一个锁,所以循环等待它返回
true
。这是它返回
false
的唯一原因
LOCK\u NB
是指如果您未能打开文件并将其作为第一个参数传递给
null
,等待肯定不会有帮助……循环意味着在这种情况下,您将在无限循环中发出警告。@MarkAmery我从来都不是PHP专家,尤其是在2013年,我刚开始ca时里尔,所以这肯定是一个初学者的错误:)谢谢你指出这一点!