0

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!

2
  • Have a look at expect: expect.nist.gov Commented Oct 19, 2009 at 15:09
  • You can't nest commands in ncftp, right? Like: ncftp -u username -p password && put ... && put... Commented Oct 19, 2009 at 15:14

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

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.
Out of curiosity, what part is expensive? Connecting? Do you need to connect to multiple hosts? Are transfers actually slower?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.