Bluefire
Bluefire

Reputation: 14109

file_put_contents stops my code from executing

I want to send some data as a GET request to my php page (submit.php) and save it in a local file. I have:

$loc = 'data.txt';
if(isset($_GET["data"])) {
    echo 'set';
    file_put_contents($loc, $_GET["data"], FILE_APPEND);
}
echo 'foo';

But when I access submit.php?data=bar, nothing happens to data.txt; moreover, echo 'foo' does not seem to execute. Why is this?

Upvotes: 1

Views: 543

Answers (1)

axiac
axiac

Reputation: 72226

echo 'foo' does not execute because file_put_contents() encounters an error and the execution stop.

Put error_reporting(E_ALL & ~E_NOTICES); ini_set('display_errors', '1'); in front of your script to let PHP display the errors on screen.

This way you can find that, I guess, the process that runs the PHP code (the web server probably) does not have the rights to write in the directory where you store the code.

Change $loc to '/tmp/data.txt' and it will work. Or, even better, create a new directory, set its permissions to rwx for everybody and change the code to write files in it.

Upvotes: 2

Related Questions