PHP: FTP functions - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<fribidi_log2visftp_cdup>
view the version of this page
Last updated: Fri, 18 Apr 2003

XXXIII. FTP functions

�vod

The functions in this extension implement client access to file servers speaking the File Transfer Protocol (FTP) as defined in .

Po�adavky

Tyto funkce jsou k dispozici jako sou��st standardn�ho modulu, kter� je v�dy dostupn�.

Instalace

In order to use FTP functions with your PHP configuration, you should add the --enable-ftp option when installing PHP 4 or --with-ftp when using PHP 3.

Verze PHP pro Windows m� vestav�nou podporu pro toto roz���en�. K pou�it� t�chto funkc� nen� t�eba na��tat ��dn� dal�� roz���en�.

Konfigurace b�hu

Toto roz���en� nem� definov�no ��dn� konfigura�n� direktivy.

Typy prost�edk�

This extension uses one resource type, which is the link identifier of the FTP connection, returned by ftp_connect().

P�eddefinovan� konstanty

Tyto konstanty jsou definov�ny t�mto roz���en�m a budou k dispozici pouze tehdy, bylo-li roz���en� zkompilov�no spole�n� s PHP nebo dynamicky zavedeno za b�hu.

FTP_ASCII (integer)

FTP_TEXT (integer)

FTP_BINARY (integer)

FTP_IMAGE (integer)

FTP_TIMEOUT_SEC (integer)

See ftp_set_option() for information.

The following constants were introduced in PHP 4.3.0.

FTP_AUTOSEEK (integer)

See ftp_set_option() for information.

FTP_AUTORESUME (integer)

Automatically determine resume position and start position for GET and PUT requests (only works if FTP_AUTOSEEK is enabled)

FTP_FAILED (integer)

Asynchronous transfer has failed

FTP_FINISHED (integer)

Asynchronous transfer has finished

FTP_MOREDATA (integer)

Asynchronous transfer is still active

P��klady

P��klad 1. FTP example

<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        exit; 
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

// close the FTP stream 
ftp_close($conn_id); 
?>

Obsah
ftp_cdup -- Changes to the parent directory
ftp_chdir -- Changes directories on a FTP server
ftp_chmod -- Set permissions on a file via FTP
ftp_close -- Closes an FTP connection
ftp_connect -- Opens an FTP connection
ftp_delete -- Deletes a file on the FTP server
ftp_exec -- Requests execution of a program on the FTP server
ftp_fget -- Downloads a file from the FTP server and saves to an open file
ftp_fput -- Uploads from an open file to the FTP server
ftp_get_option -- Retrieves various runtime behaviours of the current FTP stream
ftp_get -- Downloads a file from the FTP server
ftp_login -- Logs in to an FTP connection
ftp_mdtm -- Returns the last modified time of the given file
ftp_mkdir -- Creates a directory
ftp_nb_continue -- Continues retrieving/sending a file (non-blocking)
ftp_nb_fget -- Retrieves a file from the FTP server and writes it to an open file (non-blocking)
ftp_nb_fput -- Stores a file from an open file to the FTP server (non-blocking)
ftp_nb_get -- Retrieves a file from the FTP server and writes it to a local file (non-blocking)
ftp_nb_put -- Stores a file on the FTP server (non-blocking)
ftp_nlist -- Returns a list of files in the given directory
ftp_pasv -- Turns passive mode on or off
ftp_put -- Uploads a file to the FTP server
ftp_pwd -- Returns the current directory name
ftp_quit -- Alias of ftp_close()
ftp_raw -- Sends an arbitrary command to an FTP server
ftp_rawlist -- Returns a detailed list of files in the given directory
ftp_rename -- Renames a file on the FTP server
ftp_rmdir -- Removes a directory
ftp_set_option -- Set miscellaneous runtime FTP options
ftp_site -- Sends a SITE command to the server
ftp_size -- Returns the size of the given file
ftp_ssl_connect -- Opens an Secure SSL-FTP connection
ftp_systype -- Returns the system type identifier of the remote FTP server


User Contributed Notes
FTP functions
add a note
Paul Southworth
16-Mar-2000 09:27

If you are looking for a sketchy sample implementation of an FTP client written in PHP, here is a free one:  
sean at debugs dot org
26-Dec-2000 08:21

Be careful when using ftp_size; some ftp servers report different results depending on whether you have issued a BINARY or ASCII command first.   This is a bit of a drag because there appears to be no way of issuing either command with these functions.   Instead you must specify the desired transfer type with each ftp_get or ftp_put.   Thus for some servers you will get a different number of bytes actually transfered than that reported by the ftp_size function....... Yuk....
tgrk at hushmail dot com
25-Jan-2002 10:25

In case that your ftp connection using php failed(error msg is ftpbuf()..) add first line before setting up ftp connection:

$conn_id=0;
$conn_id = ftp_connect("$ftp_server");

sven at cartell-network dot de
13-Feb-2002 06:27

connection to a ftp server across proxy

$ftp_server = "proxy"; f.e. 123.456.789.10
$ftp_user_name = "username@ftpserver"; f.e. [email protected]
$ftp_user_pass = "password";

$conn_id = ftp_connect($ftp_server, 2121);
$login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );

o_sheep at hotmail dot com
13-Mar-2002 07:15

you can try using

set_time_limit(0);  which will reset the timeout.

to find out what has been completed at at the php script.. you can try using flush(); after echo.. which should display your messages without having to wait for the script to finish loading..

"..I've got this working fine but with slow sites and when sending a batch of
files, the PHP times out! I am using a commercial host so cannot tell them
to increase the PHP timeout and in any case I think that isn't
appropriate.

I am thinking in terms of first opening a connection and then sending each
file with a reload of the page (using a parameter to increment the file to
be sent). This could also give a "progress" indication rather
than freezing.

Is this sensible or can anyone suggest a better approach?

Thanks.

Neil..."

will2025 at yahoo dot com
13-May-2002 10:51

Si teneis como respuesta ftpbuf 0 probar esto para conectar:
$conn_id = ftp_connect("127.0.0.1");
Parece absurdo pero me ha funcionado en 2 servidores.

ftpbuf 0 ??? Try:
$conn_id = ftp_connect("127.0.0.1");
It works!!

NOSPAMkent at ioflux dot NOSPAM dot com
20-Sep-2002 12:05

I think what some other posts were trying to say which may need clarification is that in PHP 4.2.3, ftp_connect("myhost.com") was failing most of the time, except it would work like every few minutes.

The fix is that ftp_connect seems to have a bug resolving addresses. If you do:

$hostip = gethostbyname($host);
$conn_id = ftp_connect($hostip);

It seems to solve the problem.
(Other users referred to an ftpbuf() error... not sure what that is, but this should fix it.)

(nospam) bool AT boolsite DOT net
10-Oct-2002 05:19

If you search for FXP (server to server FTP transfer), look at .

This easy to use class is now able to do FXP transfer (v0.6.2) !!!!

Marius at lowvoice dot nl
22-Oct-2002 09:51

if you need to download a  file into a  string instead of into a file try this:

$temp = tmpfile();

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $user, $pass);

$fget_result=ftp_fget($conn_id, $temp, $ftp_file_name, FTP_ASCII);
ftp_quit($conn_id);

rewind ($temp);
$FILE_DATA=fread($temp,$size);
fclose($temp);

you could also do this with fopen() but I found that using this function will keep the connection open even after a fclose()!! So repeating this many times will cause a lot of open ftp-connections.

vikas dot saxena at 4cplus dot com
25-Nov-2002 10:09

While using ftp_rmdir($conn, $userdir);
I'm getting the following error msg.

Unable to find ftpbuf 0

this message is showing where ever $conn is being used.

eric at evilwalrus dot com
11-Jan-2003 05:14

There is no FTP command for the CHMODing of files, so you really have to do it manually using ftp_exec(). I havn't tried this myself but I'm assuming it should work, but here is an example of what I would do.

//After we have connected to the FTP server
//and need to set the perms for a file/dir, use
//this command
if (ftp_exec($ftp_conn, 'chmod 0666 logs/log.txt'))
   echo 'Successfully set permissions for logs/log.txt to 0666';
else
   echo 'Failed to set permissions for logs/log.txt';

Again, I have not tested this, but I assume it would work. Hope that helps.

php dot net at thedrewerys dot com
08-Feb-2003 10:25

To chmod, you need to use ftp_site instead of ftp_exec.

In the example above,
ftp_exec($ftp_conn, 'chmod 0666 logs/log.txt')
should be
ftp_site($ftp_conn, 'chmod 0666 logs/log.txt')

arjenjb dot wanadoo dot nl
09-Mar-2003 05:29

Check for a FTP server written in PHP.
Supports Passive and Active FTP, and all other standard FTP commands as decribed in RFC959.

add a note

<fribidi_log2visftp_cdup>
 Last updated: Fri, 18 Apr 2003
show source | credits | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Sat May 10 05:10:01 2003 CEST