I'm running a shell command ( its a web scraper ) via php exec ( although, i have tried system and passthru as well) and i need to save the results in a file, (preferably .txt),The output data is some HTML. it creates the file, but its always empty.
please help me out.
Below is the code, im trying to run
file_put_contents('data.php',passthru('casperjs the_file_in_which_i_run.js',$output));
also tried
file_put_contents('data.php',exec('casperjs the_file_in_which_i_run.js',$output));
and
file_put_contents('data.php',system('casperjs the_file_in_which_i_run.js',$output));
Redirect Output to a File Only To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.
You have to create a pipe from the parent process to the child, using pipe() . Then you must redirect standard ouput (STDOUT_FILENO) and error output (STDERR_FILENO) using dup or dup2 to the pipe, and in the parent process, read from the pipe. It should work.
exec(...., $output);
file_put_contents('file.txt', $output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With