tags:

views:

39

answers:

1

Let's say I'm connecting to an FTP by sending an ajax request to PHP:

$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);

After this request I want to upload a file:

$upload = ftp_put($connection, $dest, $source, $mode);

Is there a way to keep an FTP connection to the server on the client side and keep sending ajax requests to it without reestablishing the FTP connection on the server side?

Thank you!

+1  A: 

Hmm.. the only way that comes to mind is forking the PHP process, and then modifying some shared resource (file, shmem, etc) to pass messages to/from the AJAX process and the long-running FTP process.

But to be quite frank, you might want to reconsider your problem/tool combination here. It feels a bit like the old addage "If all you've got is a hammer, every problem starts to look like a nail". PHP's main strength is that it is specifically geared towards handling a single request and cleaning up all resources it claimed after handling the request. Other languages/platforms are more persistent, and might be more suitable for what you're trying to do here. I'm thinking along the lines of Java or .Net here.

kander
How would Java or .Net be more helpful in this situation? Once an ajax request is complete, I will have to start a new request and create a new FTP process. Or am I wrong?I was thinking about somehow passing an FTP connection id to the ajax part and keeping it there until the next request. But in this case I'm not exactly sure whether PHP will close an FTP connection before my next request. And also I'm not sure how to get the FTP connection id, since it's a PHP "resource."It's not a crucial problem though, since I can just open and close FTP connection every time I make an ajax request.
John Space
Perhaps my answer should start with "No, unless..."The question you ask stems from the fact that PHP is a very request-centered language, so no - there is no way to "transfer" the connection from one request to another (at least: as far as I know). If the FTP connection is created in request A, then it will die along with request A.Other languages/platforms allow you to create resources that are not directly tied to a request, which is why they would help with this.Knowing this - the only solution I can think of is creating a separate process, not tied to your current request: forking
kander
"If the FTP connection is created in request A, then it will die along with request A."—very good and clear point. Thank you!
John Space
You're welcome! It's an interesting idea you touch upon, AJAX-FTP. Especially when coupled with the new Drag and Drop file upload mechanisms enabled by HTML5. Exciting times we're living in!
kander