tags:

views:

78

answers:

3

Hi,

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!

A: 

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.

dimitris mistriotis
+1  A: 

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);
Tom Haigh
Thank you. It seems proc_open() might be right.
Christoffer
+1  A: 

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

Chris Kloberdanz
I have built a working implementation using PHP's built-in FTP already but it is very much slower than running NcFtp via shell execution.
Christoffer
Out of curiosity, what part is expensive? Connecting? Do you need to connect to multiple hosts? Are transfers actually slower?
Chris Kloberdanz