11

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?

chris-l
  • 2,802
  • 1
  • 18
  • 18
Sanjay Rathod
  • 1,083
  • 5
  • 18
  • 45
  • You'd rather execute it in another process, invoking the shell and all that, than simply `include` it? – cHao Dec 09 '13 at 05:12
  • You can say `include "other_script.php";` to run the script within the same PHP instance, rather than spawning another interpreter. There are valid reasons to want a distinct process...but unless you have to, `include` is usually better. – cHao Dec 09 '13 at 05:16
  • No i have to run through it shell because i want to make an php online editor – Sanjay Rathod Dec 09 '13 at 05:18
  • I had tried this $script_output = shell_exec("php $myfile 2> output"); but this code return nothing. – Sanjay Rathod Dec 09 '13 at 05:22
  • 1
    If you have to ask this question here, then you are not ready to write an online php editor and deal with all the security issues this will raise. – vascowhite Dec 09 '13 at 05:23
  • [exec function](http://php.net/function.exec)? well, to just run php code you could also use [eval](http://www.php.net/eval) – Huey Dec 09 '13 at 05:23
  • no i have not tried that – Sanjay Rathod Dec 09 '13 at 05:25
  • Are you trying to have one php file execute another php file and anything it writes have it write into a buffer? – Randy Dec 09 '13 at 05:27
  • Yes ut writes in buffer and i am taking the output from buffer by this code ob_get_contents(); – Sanjay Rathod Dec 09 '13 at 05:29
  • I added an answer below but I'm still not sure if that's what you need. – Randy Dec 09 '13 at 05:35
  • I think that instead of asking how to achieve this (for what you have a lot of valid answers), you should go one step back and explain the scenario, and what functionality you need, since i bet that this is not a correct approach for whatever you are trying to do, and probably you will have many problems in the future – Carlos Robles Jan 15 '14 at 03:17

7 Answers7

7

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<<<<
Randy
  • 4,351
  • 2
  • 25
  • 46
  • Hi 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,""."\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 – Sanjay Rathod Dec 09 '13 at 05:46
  • 1
    I'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? – Randy Dec 11 '13 at 13:33
4

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

James Binford
  • 2,753
  • 1
  • 14
  • 12
2

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.

sagunms
  • 8,030
  • 5
  • 41
  • 43
1

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.

Sandeep
  • 956
  • 2
  • 9
  • 19
1

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
================
towr
  • 4,147
  • 3
  • 20
  • 30
0
terminal view:
abgth@ubuntu:/var/www$ cat > test.php

  <?php
      echo shell_exec("php5 /var/www/check.php");     
  ?>

abgth@ubuntu:/var/www$ cat > check.php
  <?php
      echo 'hi';
  ?>
abgth@ubuntu:/var/www$ php5 test.php
hi

I hope you are looking for this

Abhijith A C
  • 530
  • 2
  • 7
  • 21
-3

Go to terminal and type

php filename.php

filename.php should be the name of file you want to execute!