1

The command below is a shell command that works for me.

php -f /home3/kintest2/www/project/keygen_msql_adjuster.php

What i would like to do is the make this command run from a php file.

Any hints are welcomed

2
  • 1
    Here're two hints: exec, shell_exec. Commented Jul 20, 2012 at 14:46
  • shell_exec() Commented Jul 20, 2012 at 14:46

4 Answers 4

1

What's wrong with:

include('/home3/kintest2/www/project/keygen_msql_adjuster.php');

or is there a particular reason you want to fire off a whole new PHP interpreter instance?

I'd note that a good reason not to use include is because anything declared therein will pollute your current instance's namespace.

On the flip side, calling shell_exec or similar can be a massive security hole. If your code is running in a restricted environment that may not matter, though.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to execute php from another php file. What the file does is it updates only one database record that satisfies some conditions. But I want the file to continuously execute this file in a loop
1

You can use backticks or system or shell_exec or popen, depending on what you want to do with the process you start.

2 Comments

I'm trying to execute php from another php file. What the file does is it updates only one database record that satisfies some conditions. But I want the file to continuously execute this file in a loop
Are think of some kind of a scheduled job, that would kick in in every few minutes/hours? If you are, take a look at en.wikipedia.org/wiki/Cron
0

I think you are looking for shell_exec:

echo shell_exec ( "php -f /home3/kintest2/www/project/keygen_msql_adjuster.php" );

2 Comments

This code works well for any other command. When I use the shell command above it doesn't do anything the page just seems to load for every... Any ideas on why ?
@CongoleseMedia Could potentially be a permissions problem on that file or folder, or it could be an environment problem, like having safe_mode enabled.
0

You might also consider using a command-running library that handles errors and things properly: https://github.com/kamermans/command

Then you can run the program like this:

use kamermans\Command\Command;
$cmd = Command::factory('php')
    ->option('-f', 'keygen_msql_adjuster.php')
    ->setDirectory('/home3/kintest2/www/project')
    ->run();

Or, more simply put:

use kamermans\Command\Command;
$cmd = Command::factory('php -f /home3/kintest2/www/project/keygen_msql_adjuster.php');

Unlike PHPs built-in methods, this method will throw a PHP Exception if the command fails, which you can then catch and handle if need be:

use kamermans\Command\Command;
use kamermans\Command\CommandException;

$cmd = Command::factory('php')
    ->option('-f', 'keygen_msql_adjuster.php')
    ->setDirectory('/home3/kintest2/www/project');

try {
    $cmd->run();
} catch (CommandException $e) {
    die("The command failed: ".$e->getMessage()."\n");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.