Jan
Jan

Reputation: 397

Save the console text into a txt file? (PHP)

actual I finished writing my program. Because it is only a plugin and it runs on a external server I still want to see if I get some errors or something else in the console.

I wrote every console input with echo ...;. My question now is if it is possible to get the text of the console?

Because then I could easily safe it in a .txt file and could get access to it from the web :) - Or is there another way to get the console text?

I could probably just say fwrite(...) instand of echo ...;. But this will cost a lot of time...

Greetings and Thank You!

Upvotes: 0

Views: 1652

Answers (4)

Curlas
Curlas

Reputation: 899

For console (commando line interface) you can redirect the output of your script:

php yourscript.php > path-of-your-file.txt

If you haven't access to a command line interface or to edit the cronjob line, you can duplicate the starndar output at the begining of the script:

$fdout = fopen('path-to-your-script.txt', 'wb');
eio_dup2($fdout, STDOUT);
eio_event_loop();
fclose($fdout);

(eio is an pecl extension)

Upvotes: 1

Pablo Delgado
Pablo Delgado

Reputation: 64

An alternative that could be usefull on windows would be to save all the output buffer to a txt, first check your php configuration for the console app implicit_flush must be off then

<?php  
ob_start(); //before any echo
/** YOUR CODE HERE **/
$output = ob_get_contents(); //this variable has all the echoes
file_put_contents('c:\whatever.txt',$output);
ob_flush(); //shows the echoes on console
?>

Upvotes: 1

Lucas Krupinski
Lucas Krupinski

Reputation: 694

If your goal is to create a text file to access, then you should create a text file directly.

(do this instead of echoing to console)

$output  = $consoleData . "\n";
$output .= $moreConsoleData . "\n"; 

(Once you've completed that, just create the file:)

$file = fopen('output.txt', 'a');
    fwrite($file, $output);
    fclose($file);

Of course, this is sparse - you should also check that the file exists, create it if necessary, etc.

Upvotes: 1

Niraj Shah
Niraj Shah

Reputation: 15457

If you are running the script using the console (i.e. php yourscript.php), you can easily save the output my modifying your command to:

php yourscript.php > path/to/log.txt

The above command will capture all output by the script and save it to log.txt. Change the paths for your script / log as required.

Upvotes: 0

Related Questions