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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: