Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 476
abhi Avatar asked Dec 13 '25 08:12

abhi


2 Answers

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);
like image 152
eol Avatar answered Dec 15 '25 00:12

eol


try standard bash notation:

php script.php > file.txt
like image 21
Christian Cerri Avatar answered Dec 15 '25 02:12

Christian Cerri