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

GETting It Right - 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


Now that you know where you are and who's around you, it's time to start moving around in the directory tree - and the function that lets you do that is ftp_chdir(), which accepts a directory name as parameter.

<? // change directory to "public_html" ftp_chdir($conn, "public_html"); ?>
If all you want to do is go up to the parent directory, an alternative way of accomplishing this is the ftp_cdup() command, which transports you one level up from wherever you are currently.

<? // go up one level in the directory tree ftp_cdup($conn); ?>
You can also create and remove directories with ftp_mkdir() and ftp_rmdir(); note that ftp_mkdir() returns the name of the new directory if successful.

<? // make the directory "test" ftp_mkdir($conn, "test"); // remove the directory "test" ftp_rmdir($conn, "test"); ?>
The purpose of opening an FTP session is usually to transfer files - so let's get to that!

Uploading a file is accomplished by means of the ftp_put() command, which requires you to specify a name for the file on the remote system, the name of the file to be uploaded, and the type of transfer. For example, if you'd like to upload the file "abc.txt" and rename it to "xyz.txt" on the remote system, the command would look something like this:

<? // upload ftp_put($conn, "xyz.txt", "abc.txt", FTP_ASCII); ?>
Alternatively, if you'd like to download a file, the built-in function is ftp_get(), which also needs both remote and local file names, although the order is reversed. If the file to be downloaded is named "his.zip" on the remote system and you plan to save it as "hers.zip" on the local system, the syntax would change to

<? // download ftp_get($conn, "hers.zip", "his.zip", FTP_BINARY); ?>
PHP defines two modes for file transfer - FTP_BINARY and FTP_ASCII - which need to be specified as they are displayed here, without quotes. Note that both ftp_get() and ftp_put() return result codes, indicating whether or not the transfer was successful.

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: