Question

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.

Was it helpful?

Solution

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

if ($pos === false) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top