2

我之前问过一个问题(如何防止这个计数器重置为 100,000?),现在有一个后续问题。

我有另一个版本的计数器,可以被告知重置为某个数字,我想确保第二个版本与第一个版本没有相同的问题。

我现在编码的是:

$reset = '10';

$filename4 = "$some_variable/$filename3.txt";  

// Open our file in append-or-create mode.
$fh = fopen($filename4, "a+");

if (!$fh)
    die("unable to create file");

if ($reset == 'default'){        
    // Before doing anything else, get an exclusive lock on the file.
    // This will prevent anybody else from reading or writing to it.
    flock($fh, LOCK_EX);

    // Place the pointer at the start of the file.
    fseek($fh, 0);

    // Read one line from the file, then increment the number.
    // There should only ever be one line.
    $current = 1 + intval(trim(fgets($fh)));

    // Now we can reset the pointer again, and truncate the file to zero length.
    fseek($fh, 0);

    ftruncate($fh, 0);

    // Now we can write out our line.
    fwrite($fh, $current . "\n");

    // And we're done.  Closing the file will also release the lock.
    fclose($fh);                
}
else {        
    $current = trim(file_get_contents($filename4)) + 1;

    if($current >= $reset) {            
        $new = '0';            
        fwrite(fopen($filename4, 'w'), $new);                        
    }
    else {            
        fwrite(fopen($filec, 'w'), $current);                        
    }               
}

echo $current;

我不想假设我知道要对此代码进行哪些更改,所以我发布了另一个问题。编辑-如果 $reset 不等于默认值,我应该在此处进行哪些更改以避免在文件上获得独占锁定?对此进行编码的正确方法是什么?这会工作吗?:

$filename4 = "$some_variable/$filename3.txt";  

// Open our file in append-or-create mode.
$fh = fopen($filename4, "a+");

if (!$fh)
die("unable to create file");


// Before doing anything else, get an exclusive lock on the file.
// This will prevent anybody else from reading or writing to it.
flock($fh, LOCK_EX);

// Place the pointer at the start of the file.
fseek($fh, 0);



if ($reset == 'default'){

// Read one line from the file, then increment the number.
// There should only ever be one line.
$current = 1 + intval(trim(fgets($fh)));

} else {
// Read one line from the file, then increment the number.
// There should only ever be one line.
$current = 1 + intval(trim(fgets($fh)));

if($current >= $reset) {            
    $current = '0';            
}
else {            
// Read one line from the file, then increment the number.
// There should only ever be one line.
$current = 1 + intval(trim(fgets($fh)));

} 
}
// Now we can reset the pointer again, and truncate the file to zero length.
fseek($fh, 0);

ftruncate($fh, 0);

// Now we can write out our line.
fwrite($fh, $current . "\n");

// And we're done.  Closing the file will also release the lock.
fclose($fh);                


echo $current;

编辑 - 这似乎对我有用:

$reset = "default";
$filename4 = "counter.txt";  

// Open our file in append-or-create mode.
$fh = fopen($filename4, "a+");

if (!$fh)
die("unable to create file");


// Before doing anything else, get an exclusive lock on the file.
// This will prevent anybody else from reading or writing to it.
flock($fh, LOCK_EX);

// Place the pointer at the start of the file.
fseek($fh, 0);

// Read one line from the file, then increment the number.
// There should only ever be one line.
$current = 1 + intval(trim(fgets($fh)));

if ($reset == 'default'){
$new = $current;
} else {
if($current >= ($reset + '1')) {            
$new = '1';            
}
else {            
$new = $current;
} 
}
// Now we can reset the pointer again, and truncate the file to zero length.
fseek($fh, 0);

ftruncate($fh, 0);

// Now we can write out our line.
fwrite($fh, $new . "\n");

// And we're done.  Closing the file will also release the lock.
fclose($fh);                


echo $new;

这看起来对吗?

4

3 回答 3

2
    if($current >= $reset) {
        // here is where you are setting the counter back to zero. comment out
        // these lines.
        //$new = '0';            
        //fwrite(fopen($filename4, 'w'), $new);                        
    }

如果您只是想要一个不会重置的计数器,请尝试:

$filename4 = "counter.txt";  

// Open our file in append-or-create mode.
$fh = fopen($filename4, "a+");

if (!$fh)
die("unable to create file");


// Before doing anything else, get an exclusive lock on the file.
// This will prevent anybody else from reading or writing to it.
flock($fh, LOCK_EX);

// Place the pointer at the start of the file.
fseek($fh, 0);

// Read one line from the file to get current count.
// There should only ever be one line.
$current = intval(trim(fgets($fh)));

// Increment
$new = $current++;

// Now we can reset the pointer again, and truncate the file to zero length.
fseek($fh, 0);

ftruncate($fh, 0);

// Now we can write out our line.
fwrite($fh, $new . "\n");

// And we're done.  Closing the file will also release the lock.
fclose($fh);

echo $new;                
于 2011-03-09T20:10:00.803 回答
1

我能看到的最好的方法是打开文件以使用除独占之外的锁来读取。然后您可以执行所需的检查,如果计数超过 $reset 值,您可以关闭文件,再次打开它,但这次使用排他锁进行写入。另一种方法就是不使用排他锁。您可以查看已经测试过锁定机制的非常好的平面文件类。

于 2011-03-09T21:09:42.200 回答
1

file_put_contents已经是原子的了。不需要十行文件锁定代码。

<?php

    $fn = "$filename3.txt"; 

    $reset = 0;        // 0 is equivalent to "default"
    //$reset = 10000000;  

    $count = file_get_contents($fn);        
    $count = ($reset && ($count >= $reset)) ? (0) : ($count + 1);
    file_put_contents($fn, $count, LOCK_EX);

    echo $count;

不知道这是否有帮助,因为您的问题仍然不透明。我不会回答评论。

于 2011-03-09T21:19:37.947 回答