Deimantas
Deimantas

Reputation: 67

PHP - file_put_contents is not working

This is my code:

<?php

$name = $_GET['name'];
$ref = $_SERVER['HTTP_REFERER'];
$number1 = trim(file_get_contents("{$name}.txt"));

$number2 = ($number1++);
file_put_contents("$name.txt", $number2);

echo("$name $ref $number1 $number2");
echo("Thanks for your like ;)");
echo("<a href='$ref'>Back to page you were viewing</a>");
?>

file get contents is working properly, since the echo tells that to me, but the problem is the file put contents which doesn't put the content into file. Everything else is fine.

i've set the txt file that the script created to 0777 permissions

Upvotes: 0

Views: 851

Answers (1)

Dexter Bengil
Dexter Bengil

Reputation: 6625

$number2 = ($number1++);

This is a post increment operation, because of that, $number2 won't get changed until the next increment. So change it to a pre increment below:

 $number2 = ++$number1;

so the increment will happen directly.

/** post increment */
$a = 5;
$b = $a++; // $a= 6; $b = 5;

/** pre increment */
$c = 5;
$d = ++$c; // $c = 6; $d = 6;

Upvotes: 1

Related Questions