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?

有帮助吗?

解决方案

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

其他提示

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

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.

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.

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

Go to terminal and type

php filename.php

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top