Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save the exec output into a file

Tags:

shell

php

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));
like image 955
Mohammad Avatar asked Feb 16 '14 08:02

Mohammad


People also ask

How do I write output of a command file?

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.

How do you find the output of an exec?

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.


1 Answers

exec(...., $output);
file_put_contents('file.txt', $output);
like image 91
Marc B Avatar answered Sep 19 '22 17:09

Marc B