开发者

Basic PHP file manipulation

Apologizes for such basic question, but I've gone mad debuggind the following code:

$fname = 'results.txt';

$handle = fopen($fname,"a+");
if ($handle){
    $cnt = file_get_contents('./results.txt');
    $pos = strpos($cnt,":");
    if ($pos === 'false'){
        $str = htmlspecialchars($_COOKIE['username']).": ".$_COOKIE['score'];
        fwrite($handle,$str);
        }
    if ($cnt) echo $开发者_开发技巧cnt;
    else echo 'Error in file_get_contents!<br />';
}
else echo '<span>Error while opening file</span>';

$cnt returns false, whatever I do. I've tried to change the argument to 'results.txt', full url to file - still no progress. I looked up the function on php.net, and from what I see, syntax is correct.

Thanks for your time.

P.S. Code itself is not nice (f.e. regular expressions would suit better for this task), as I wrote it in haste, but I want to get it running before rewriting.


strpos returns boolean false, not the string value of 'false'. Try this:

if ($pos === false) {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜