Using FTP


FTP is great for transferring files back and forth on the Internet, and you can use FTP from PHP. In order to use FTP functions, you should add the --enable-ftp option when installing PHP 4. The Windows version of PHP has built-in support for this extension.

Using FTP is not hard. To connect to an FTP server, you use ftp_connect:

 ftp_connect(string host [, int port [, int timeout]]) 

This function opens an FTP connection to the specified host (note that host shouldn't have any trailing slashes and shouldn't be prefixed with ftp://). The port parameter specifies an alternate port to connect to; if it is omitted or set to zero, then the default FTP port, 21, will be used. The timeout parameter specifies the timeout for all subsequent network operations. If omitted, the default value is 90 seconds. This function returns an FTP stream on success or FALSE on error.

After you connect with ftp_connect, you must log in with ftp_login, which you use this way:

 ftp_login(resource ftp_stream, string username, string password) 

This function passes your username and password to the FTP server, and it returns trUE on success or FALSE on failure.

For example, here's how you can connect to and log into an FTP server:

 <?php     $connect = ftp_connect("ftp.ispname.com");     $result = ftp_login($connect, $username, $password);     .     .     . 

We'll use this kind of code to connect to FTP servers in the following chunks. Besides ftp_connect and ftp_login, there are plenty of other FTP functions in PHP; here's a starter list:

  • ftp_alloc. Allocates space for a file to be uploaded

  • ftp_cdup. Changes to the parent directory

  • ftp_chdir. Changes directories on an FTP server

  • ftp_chmod. Sets 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 run-time behaviors 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

  • ftp_nb_fget. Retrieves a file from the FTP server and writes it to an open file

  • ftp_nb_fput. Stores a file from an open file to the FTP server

  • ftp_nb_get. Retrieves a file from the FTP server and writes it to a local file

  • ftp_nb_put. Stores a file on the FTP server

  • ftp_nlist. Returns a list of files in the given directory

  • ftp_put. Uploads a file to the FTP server

  • ftp_pwd. Returns the current directory name

  • ftp_quit. An 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. Sets miscellaneous run-time FTP options

  • ftp_site. Sends a SITE command to the server

  • ftp_size. Returns the size of the given file

  • ftp_ssl_connect. Opens a secure SSL-FTP connection

  • ftp_systype. Returns the system type identifier of the remote FTP server

We'll put a number of these functions to work in the following chunks.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net