1

I have a multi-user PHP web application that can interact with an FTP server via AJAX. The application allows the user to browse an FTP site. Javascript makes an AJAX call which communicates with a server-script that returns a list of files and directories within a given directory.

This works fine. However, each time a directory listing is requested, the server must re-establish a connection with the FTP server, which takes a lot of time.

I need to persist an FTP connection PHP resource across AJAX calls. In other words, the connection must remain open, and I must be able to run ftp_nlist() using that resource, without re-establishing the connection or re-authenticating, with each new AJAX call (until the connection times out, of course).

Can anyone think of a way to do this?

Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
  • 1
    You would have to be able to make a singleton type ftp class and store it some place. The only place to keep the connection object is in the Apache memory and I think that would mean a custom extension. Sounds like a good project if it is possible. – Clutch Dec 17 '10 at 21:18

4 Answers4

2

I don't think it's possible using the FTP library in PHP. I see somebody even had a feature request for it in PHP, but it doesnt seem there was any action taken on it.

The only way I can think of is to use a 3rd-party FTP client that keeps the connection open and interface with it through PHP. (instead of a 3rd party ftp client, you could just use the FTP functions built-in to the OS. Windows provides them, as does Linux through the "ftp" program.)

simshaun
  • 21,263
  • 1
  • 57
  • 73
  • Does anyone know of any such 3rd-party libraries? – Chad Johnson Dec 17 '10 at 20:47
  • Check out the CLI clients listed on http://linuxreviews.org/software/ftp-clients/ – simshaun Dec 17 '10 at 20:49
  • My app will need to be able to manage dozens of simultaneous FTP connections. So multiple users can connect to different servers. Is there a tutorial/article for using these CLI clients in a PHP application? – Chad Johnson Dec 17 '10 at 21:51
  • May not be a tutorial for this specific instance, but in general, to execute any external program with PHP, you use the exec() function. `exec('someftpprogram -arguments -go -here')` You just have to fill in the commands for doing what you need as if you were running the FTP client through the terminal window. – simshaun Dec 17 '10 at 22:01
  • The thing I'm wondering about, though, is, if I was to use exec() + a CLI program, how would I continue using a connection once it's created? – Chad Johnson Dec 17 '10 at 22:11
1

Sorry to add clutter without a clear answer for ya but this might be helpful: http://www.eecho.info/Echo/php/client-url-library-php-curl/ It appears you are in control of opening and closing the connections however in terms of returning this variable to the client and having it re-used I'm not sure that is possible (also it might just clean itself up out of your control), alternatively you might (depending on the end environment) consider using a Java backend, you could code up a simple server and just add the FTP code on top (mmm... cake). Some examples of what you'd need to do for that are here:

http://fragments.turtlemeat.com/javawebserver.php

http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html

This assumes a pretty large amount of control of what's run in the server environment though so really depends on you owning the server basically or having full priveleges to do do what you want (like Amazon EC2 from what they advertise at least). You might be able to pull this off with Tomcat or some other JSP container and use JSPs instead of writing your own server but I don't know that you'd be able to persist the connection their either since it's sort of the same as PHP where the server generally interprets the file "on the fly" so to speak.

shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • So essentially this would be a daemon approach, where the daemon acts on behalf of the web site, as a proxy, and talks to the FTP and SFTP servers. I'd probably need some protocol for interacting with the daemon, also. Is that the direction you're thinking? – Chad Johnson Dec 17 '10 at 21:41
  • Yup so you'd have to establish some sort of protocol between your client and custom server for requesting or publishing files (luckily you're in complete control of this could use xerces.jar for XML in java, it's not so bad), I'm assuming here you have some time to work out this protocol and functionality, this is the furthest from a boxed solution I think I've ever proposed... you may also get some use out of this: http://mina.apache.org/ftpserver/ this sort of sounds like fun but also too time consuming to try myself right now, good luck. – shaunhusain Dec 17 '10 at 22:05
0

You can not create a persistent FTP connection with PHPs normal ftp classes functions. Are all users accesing the same ftp server or are you running a ftp web interface? If multiple users are connected to the same server (with the same rights) you could implement a cached solution.

Oliver A.
  • 2,870
  • 2
  • 19
  • 21
0

I ended up making this work using global variables (eg. $my_global). I have a ConnectionPooler singleton class which manages connections stored in a hash.

Chad Johnson
  • 21,215
  • 34
  • 109
  • 207