473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ftp_quit($conn_id) necessary?

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
2 1918
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: D Elkins | last post by:
I am trying to write a small web interface to allow users to upload a zip file to our server. Below is code: //The form <FORM action="ftp_upload.php" method=post enctype="multipart/form-data">...
10
by: Sylvain GRAVERON | last post by:
Hi Sorry I don't speak english very vell and no reply in fr news ! ! ! Bonjour, J'essai d'envoyer par FTP en php un fichier dont le nom, la destination et la source seront toujours les mêmes....
7
by: Markus | last post by:
Hi I encountered that FTP connections seem to remain open on the FTP server, though my FTP class has a shutdown function that ftp_quit()s the connection. I did not find any info in the manual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.