How to execute a php script from another php script by using the shell?
Solution 1
If you need to write a php file's output into a variable use the ob_start and ob_get_contents functions. See below:
<?php
ob_start();
include('myfile.php');
$myStr = ob_get_contents();
ob_end_clean();
echo '>>>>' . $myStr . '<<<<';
?>
So if your 'myfile.php' contains this:
<?php
echo 'test';
?>
Then your output will be:
>>>>test<<<<
Solution 2
You can use cURL for remote requests. The below is from php.net:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Here's a good tutorial: http://www.sitepoint.com/using-curl-for-remote-requests/
Consider watching this YouTube video here as well: http://www.youtube.com/watch?v=M2HLGZJi0Hk
Solution 3
You can try this:
Main PHP file
<?php
// change path/to/php according to how your system is setup
// examples: /usr/bin/php or /opt/lampp/bin/php
echo shell_exec("/path/to/php /path/to/php_script/script.php");
echo "<br/>Awesome!!!"
?>
Secondary PHP file
<?php
echo "Hello World!";
?>
Output when running Main PHP file
Hello World!
Awesome!!!
Hope it helps you.
Solution 4
Try this:
<?php
// change path/to/php according to how your system is setup
// examples: /usr/bin/php or /opt/lampp/bin/php
$output = shell_exec("/path/to/php /path/to/php_script/script.php");
print_r($output);
?>
This May Help you.
Solution 5
It's important to stress that including/executing user-generated code is dangerous. Using a system call (exec
, shell_exec
, system
) instead of include
helps separate the execution context, but it's not much safer. Consider proper sanitation or sand-boxing.
With that in mind, here is a working example including generating the (temporary) file, executing it, and cleanup:
<?php
// test content
$code = <<<PHP
echo "test";
PHP;
// create temporary file
$d=rand();
$myfile="$d.php";
file_put_contents($myfile,"<?php\n$code\n?>");
// start capture output
ob_start();
// include generate file
// NOTE: user-provided code is unsafe, they could e.g. replace this file.
include($myfile);
// get capture output
$result = ob_get_clean();
// remove temporary file
unlink($myfile);
// output result
echo "================\n" . $result . "\n================\n" ;
Output:
================
test
================
Sanjay Rathod
Updated on July 25, 2022Comments
-
Sanjay Rathod almost 2 years
I have a php file called
sample.php
with the following content:<?php echo "Hello World!"; ?>
And what I want to do, is to run this php script using a second php script.
I think
shell_exec
could help me, but I don't know its syntax.By the way, I want to execute this files with
cpanel
. So I have to execute the shell.Is there any way to do this?
-
Sanjay Rathod over 10 yearsIt is also return empty data. How can i find what is the error
-
Sanjay Rathod over 10 yearsHi i had tried your suggestion. It worked. But i am taking data from editor and storing that data into file using this code $d=rand(); $myfile=$d.".php"; //file_put_contents($myfile,"code: ",FILE_APPEND); file_put_contents($myfile,"<?php "."\n",FILE_APPEND); file_put_contents($myfile,$code."\n",FILE_APPEND); file_put_contents($myfile,"?>"."\n",FILE_APPEND); so you can understand from my code that each time the file name should be different. i had tried this include($myfile); but it returns an error that no such file is found. So how can i do this
-
sagunms over 10 yearsIf "Hello World!" above is not displayed, the error be displayed instead because of the "echo" before "shell_exec".
-
Randy over 10 yearsI'm not sure I see the problem. Just include the correct file. If it's named differently every time shouldn't you have a handle to the name of it somewhere?
-
raphael75 about 7 yearsHe is trying to have this execute at runtime.