1

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.

abhi
  • 55
  • 1
  • 1
  • 6
  • Have you tried anything ? – Web Artisan May 18 '16 at 12:40
  • Yes i tried so many times.Script will executing but input parameter not taking. I tried like this ./projectlist.sh $TOKEN.Here $TOKEN is the input argument for the script. – abhi May 18 '16 at 12:46
  • 1
    Related to this question? http://stackoverflow.com/questions/6543841/php-cli-getting-input-from-user-and-then-dumping-into-variable-possible – DaveK May 18 '16 at 12:59

3 Answers3

4

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);
eol
  • 23,236
  • 5
  • 46
  • 64
  • wooow This is work for me.Thanks you so much.Here output.txt not over writing every time append the content .Is it any option to overwrite. – abhi May 18 '16 at 12:57
  • @abhi [fopen()](http://php.net/manual/en/function.fopen.php), just read the documentation. – ROAL May 18 '16 at 13:00
  • 1
    fbo3264,Yes now it is working fine.Thanks for spending your valuable time. – abhi May 18 '16 at 13:30
  • You're welcome! If you like you can update my answer :) – eol May 18 '16 at 14:29
3

try standard bash notation:

php script.php > file.txt
Christian Cerri
  • 1,233
  • 2
  • 15
  • 19
  • This won't work on a website. I think a solution is needed to write echo command output to a text file from within a php file. – thepunitsingh Aug 30 '19 at 14:13
1

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);
Johny English
  • 166
  • 3
  • 6