0

I currently have this block:

if ($type == "up") {
    $lines_ = file('../indexIncomplete');
    $counter = 0;
    foreach ($lines_ as $value) {
        $postLen = strlen($post);
        if ( substr($value, 0, $postLen) === $post ) {
            break;
        }
        $counter++;
    }
    list($title, $location, $votes, $poster, $date) = explode("|", $lines_[$counter]);
    $votes = $votes + 1;
    $newValue = $title.'|'.$location.'|'.$votes.'|'.$poster.'|'.$date;
    $lines_[$counter] = $newValue;
    file_put_contents('../indexIncomplete', $lines_);
}

This is contained in the HTML page which contains the form which calls it (there is some logic above this block that does validation and stuff, I've narrowed down the problem here).

I'm pulling the contents of a file into an array. Then I look through the array to see if one of them matches $post (a string declared earlier). If it does match, the loop breaks and the counter points to the position in the array that contains the matching string. Then I grab the different parts of the string (using explode()). I grab the $value and increment it and rebuild a new array entry. The I replace the old array entry with the new one I just built on this line:

$lines_[$counter] = $newValue;

I write the new array back to the file. The problem is, the $newValue when being written doesnt seem to pick up any other variable other than $votes, making it look like this:

||1||

Where the number is the correct number of votes. It is supposed to look like this:

$title|$location|$votes|$poster|$date

Also, it doesnt appear on the line it is supposed to. Instead it is appended to the end of the file. I'm very confused.

2
  • Just before list($title, ...) line, add print($lines_[$counter]). Does it contain correct information? Commented Oct 25, 2011 at 23:40
  • No it didn't. I believe I figured it out though. It seems I can't use === to compare in the if statement. == works though. Commented Oct 25, 2011 at 23:56

2 Answers 2

2

since file_put_contents is writing to the same place from which it is read, and this is an html form, I recommend locking the file so it is not corrupted by multiple writes at once:

$file_put_contents('../indexIncomplete', $lines_, LOCK_EX);

If things still look wonky, var_dump (or log) $post as well as $lines_[$counter] to make sure your values are what you expect

Sign up to request clarification or add additional context in comments.

Comments

0

I think the issue that you are having is that the code below your foreach statement will always execute even if none of the lines in the file match your if statement.

In this situation $counter will be set to a value one higher than the last element in your array. This means you will access a non existent array key when you call "$lines_[$counter]" which will return "null" (PHP does not do array out of bounds errors like java though it will give an E_NOTICE level warning)

As the value of counter is 1 longer than the size of your array it will be appended to the end of the file.

I suggest you move this code inside your foreach before the break.

list($title, $location, $votes, $poster, $date) = explode("|", $lines_[$counter]);
$votes = $votes + 1;
$newValue = $title.'|'.$location.'|'.$votes.'|'.$poster.'|'.$date;
$lines_[$counter] = $newValue;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.