By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,442 Members | 1,485 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,442 IT Pros & Developers. It's quick & easy.

ftp_quit($conn_id) necessary?

P: n/a
Hi

I have a question that I don't find answered in the manual: After opening an
ftp connection with PHP, will it be closed automatically after the script is
executed, just as MySQL connections are? Or does it remain open unless it is
terminated with ftp_quit?

The background is that I wrote some functions that go through the directory
structure and do for example some CHMODing. The functions call themselves if
they find a directory, so I can't close the connection at the end of the
function executing:

class myclass
{

var $conn_id = false;

function ftp_connect()
{
$this->conn_id = ftp_connect("myhost");
$login_result = ftp_login($this->conn_id, "username", "password");
}

function do_something($folder)
{
if ($this->conn_id == false) {
$this->ftp_connect();
}
$contents = my_custom_directory_function($folder);
foreach ($contents as $content) {
if (is_dir($folder.$content)) {
ftp_chdir($this->conn_id, $content);
$this->do_something($folder.$content."/"); // function calls
itself
ftp_chdir($this->conn_id, "..");
}
else {
do_what_has_to_be_done();
}
}
}

}

See that there is no point where I could close the connection. Is that a
problem?

--
Markus
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Markus Ernst wrote:
I have a question that I don't find answered in the manual: After
opening an ftp connection with PHP, will it be closed automatically
after the script is executed, just as MySQL connections are? Or does
it remain open unless it is terminated with ftp_quit?

The background is that I wrote some functions that go through the
directory structure and do for example some CHMODing. The functions
call themselves if they find a directory, so I can't close the
connection at the end of the function executing:


You could add a ftp_disconnect() method, which you call at the end of your
script.

Anyways, even when you don't close the ftp resource at the end of the
script, PHP's garbage collector will get rid of it when it finishes parsing
the script.

But closing the connection when you are done is good programming practice,
so just add the ftp_disconnect() method to your class and put a call to it
right at the bottom of your script. In PHP 5 you can even add a
"__destruct()" method in your class, in which you call ftp_close(). This
method is called automatically when the script finishes.
JW

Jul 17 '05 #2

P: n/a
Janwillem Borleffs wrote:

But closing the connection when you are done is good programming
practice, so just add the ftp_disconnect() method to your class and
put a call to it right at the bottom of your script. In PHP 5 you can
even add a "__destruct()" method in your class, in which you call
ftp_close(). This method is called automatically when the script
finishes.


Thank you very much for your helpful answer. I do care about good
programming practice, but for consistency reasons I prefer to call the
ftp_close() function at the same place where the connection is established.

So with your input in mind I did some more research in the manual and found
the register_shutdown_function() function. I added

register_shutdown_function(array(&$this, 'ftp_disconnect'));

to the function that establishes the connection, which actually gives me
kind of a destructor.

--
Markus

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.