Home arrow PHP arrow Page 3 - Stream Me Up, Scotty! (part 1)

...In With The New - PHP

PHP4 comes with a bunch of new functions designed to allow FTPconnections over the Web. This article explores the new FTP functions,together with an example of how they can be used to build a browser-basedFTP client.

  1. Stream Me Up, Scotty! (part 1)
  2. Out With The Old...
  3. ...In With The New
  4. Where Am I?
  5. GETting It Right
  6. Start Me Up!
  7. Lights! Camera! Action!
  8. Well-Formed Ideas
  9. Appendix: Code Listing
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
November 07, 2000

print this article


Initiating an FTP connection in PHP follows the same basic principles: open an FTP connection, send authentication information, and then use built-in PHP functions to navigate through directories or transfer files. Let's take a look at the PHP version of the session you just saw.

<? // connect to FTP server $conn = ftp_connect("ftp.server.com"); // log in with username and password ftp_login($conn, "john", "doe"); // get remote system type ftp_systype($conn); // obtain file listing $filelist = ftp_nlist($conn, "."); // download file ftp_get($conn, "data.zip", "data.zip", FTP_BINARY); // close connection ftp_quit($conn); ?>
Let's go through this step by step:

In order to initiate an FTP connection, PHP offers the ftp_connect() function, which takes a host name and port number as parameters. In the example above, the host name is "ftp.server.com"; since a port is not specified, PHP will attempt a connection to the default FTP port, 21.

The ftp_connect() function returns a handle for the FTP connection if successful; this handle is used by all subsequent FTP functions.

<? // connect to FTP server $conn = ftp_connect("ftp.server.com"); ?>
Once connected, the ftp_login() function is used to send the FTP server a user name and password. As you can see, ftp_login() also uses the handle returned by ftp_connect() to ensure that the authentication information is transmitted to the correct server.

<? // log in with username and password ftp_login($conn, "john", "doe"); ?>
At this point, you can begin playing with some of the other functions available to you, specifically those that provide you with system information and directory listing (they're covered in detail on the next page).

Once you're finished, it's important that you remember to close your FTP session with ftp_quit().

<? // close connection ftp_quit($conn); ?>

This article copyright Melonfire 2000. All rights reserved.

>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: