12

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?

11
  • You'd rather execute it in another process, invoking the shell and all that, than simply include it? Commented Dec 9, 2013 at 5: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. Commented Dec 9, 2013 at 5:16
  • No i have to run through it shell because i want to make an php online editor Commented Dec 9, 2013 at 5:18
  • I had tried this $script_output = shell_exec("php $myfile 2> output"); but this code return nothing. Commented Dec 9, 2013 at 5:22
  • 2
    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. Commented Dec 9, 2013 at 5:23

7 Answers 7

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<<<<
Sign up to request clarification or add additional context in comments.

2 Comments

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,"<?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
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?
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

Comments

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.

2 Comments

It is also return empty data. How can i find what is the error
If "Hello World!" above is not displayed, the error be displayed instead because of the "echo" before "shell_exec".
2

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.

Comments

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
================

Comments

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

Comments

-4

Go to terminal and type

php filename.php

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

1 Comment

He is trying to have this execute at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.