2013-08-15 15:12
浏览 322


I'm trying to write to a file with php and this is the code i'm using (taken from this answer to my previous question):

$fp = fopen("counter.txt", "r+");

while(!flock($fp, LOCK_EX)) {  // acquire an exclusive lock
    // waiting to lock the file

$counter = intval(fread($fp, filesize("counter.txt")));

ftruncate($fp, 0);      // truncate file
fwrite($fp, $counter);  // set your data
fflush($fp);            // flush output before releasing the lock
flock($fp, LOCK_UN);    // release the lock


The read part works fine, if the file gets read, it's content is read well, i.e. if the file contains 2289, then 2289 is read.

The problem is that when it increments and rewrites the value to that file, [NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL]1 gets written.

What am i missing? Why do null characters get written?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用php写入文件,这是我正在使用的代码(取自< a href =“https://stackoverflow.com/questions/18236599/visits-counter-without-database-with-php/18237397?noredirect=1#answer-18237034”>这个答案到我之前的问题) :

  $ fp = fopen(“counter.txt”,“r +”); 
while(!flock($ fp,LOCK_EX)){//获取一个 独占锁
 $ counter = intval(fread($ fp,filesize(“counter.txt”))); 
 $ counter ++; 
ftruncate($  fp,0);  //截断文件
fwrite($ fp,$ counter);  //设置你的数据
fflush($ fp);  //在释放lock 
flock($ fp,LOCK_UN)之前刷新输出;  //释放锁
 nclclose($ fp); 

读取部分工作正常,如果文件被读取,它的内容读得很好,即 如果文件包含 2289 ,则读取 2289

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

我是什么 失踪? 为什么要写空字符?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doukong1391 2013-08-15 15:28

    EDIT #2:

    Try this with flock (tested)

    If file is not locked, it will throw an Exception (see added line) if(...

    I borrowed the Exception snippet from this accepted answer.

    $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
    echo file_get_contents('numbers.txt');
    解决 无用
    打赏 举报
  • dongyun3805 2013-08-15 15:32

    You can use this code, a simplified version, but am not sure if it's the best:

    $fr = fopen("count.txt", "r");
    $text = fread($fr, filesize("count.txt"));
    $fw = fopen("count.txt", "w");
    fwrite($fw, $text);
    解决 无用
    打赏 举报
  • doudu9652 2015-03-11 20:56

    The thing you are missing is rewind(). Without it, after you truncate to 0 bytes, the pointer is still not at the beginning (reference). So when you write your new value, it pads it with NULL in your file.

    This script will read a file (or create if it doesn't exist) for a current count, increments, and writes it back to the same file every time the page loads.

    $filename = date('Y-m-d').".txt";
    $fp = fopen($filename, "c+"); 
    if (flock($fp, LOCK_EX)) {
        $number = intval(fread($fp, filesize($filename)));
        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
    解决 无用
    打赏 举报

相关推荐 更多相似问题