Christoffer
Christoffer

Reputation: 26805

Keep shell open with PHP to allow multiple calls?

How can I allow the shell session to stay open until I close it with PHP?

In my example I want to use NcFtp to publish some files through shell command. I want to leave PHP's built in FTP because it is much much slower and performance is an issue.

It is easy to use ncftpput to publish a file or a directory. But if I want to loop through an array of say 10 files, the script will have to log in, publish, log out, log in, publish, log out ...

It would be much more convenient if something like this could work.

shell_exec('ncftp -u username -p password');

foreach ( $files as $file )
{
    shell_exec('put '.$file['local_path'].' '.$file['remote_path']);
}

shell_exec('quit');

Is it possible?

Thank you!

Upvotes: 0

Views: 545

Answers (3)

Dimitrios Mistriotis
Dimitrios Mistriotis

Reputation: 2788

I do not think you can do it, but what you can do is wrap the second execution in a shell script which will accept multiple parameters, or will get a feed of the file names from STDIN.
Generally these things are against php's mentality I believe (which is a web-request life span).
You can also check what part of what you want to do can be already done with existing php functions or wrappers.

Upvotes: 0

Tom Haigh
Tom Haigh

Reputation: 57815

If you just need access to one process you could probably use popen() or proc_open() to do this.

Something like this may work:

$handle = popen('ncftp -u username -p password'  , 'w');

foreach ( $files as $file ) {
    fwrite($handle, 'put ' . $file['local_path']. ' '.$file['remote_path'] . "\n");
}

pclose($handle);

Upvotes: 1

Chris Kloberdanz
Chris Kloberdanz

Reputation: 4536

It seems like this could be a job for PHP's built-in FTP functionality or Expect.

Upvotes: 1

Related Questions