Вопрос

is it possible to run multiple linux command using PHP. I am using Mongodb database and if I want to import multiple collections, I am running the following command for each collection individually.

mongoimport --db test --collection colour --file colour.json
mongoimport --db test --collection shape --file shape.json
mongoimport --db test --collection size --file size.json

Now I have at least 10 collections and I have to run each of them individually in linux command line. There should be a better way to do this. What I am thinking is to write a php script which will do this for me.

Any idea, suggestions will be really helpful. Thanks in advance.

Это было полезно?

Решение

You could have PHP create all of the shell commands beforehand and then run them all at once:

$collections = array('color', 'shape', 'size');
$command = '';

foreach($collections as $collection) {
    $command .= 'mongoimport --db test --collection ' . $collection . ' --file ' . $collection . '.json; ';
}

shell_exec($command);

This eliminates multiple calls to shell_exec(). However, perhaps mongoimport is available in the PHP mongo API.

Другие советы

You can run OS command-line commands from a PHP script using shell-exec. See http://php.net/manual/en/function.shell-exec.php.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top