abhi
abhi

Reputation: 65

How to redirect echo command output to a text file in PHP

I want to redirect echo command output to a text file.My PHP code contain echo $TOKEN;Here $TOKEN contains variable value i want to write this value into a text file. Any one can help on this.

Thanks in adavance.

Upvotes: 2

Views: 18982

Answers (3)

Johny English
Johny English

Reputation: 176

or easily with pure php:

//rewrite
file_put_contents("myfile.txt","string to file");

//add
file_put_contents("myfile.txt","string to file", FILE_APPEND);

Upvotes: 1

eol
eol

Reputation: 24565

Just use fopen/fwrite to write to a file (this will create the output.txt if it does not exist, else overwrites it)

$myfile = fopen("output.txt", "w") or die("Unable to open file!");
fwrite($myfile, $TOKEN);
fclose($myfile);

Upvotes: 5

Christian Cerri
Christian Cerri

Reputation: 1275

try standard bash notation:

php script.php > file.txt

Upvotes: 4

Related Questions