Forum Topic: Php: Basic Ftp Interactions

(949 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 3/20/06 12:57 AM

Pilot-Doofy NEUTRAL LEVEL 37

Sign-Up: 09/13/03

Posts: 12,275

PHP: Main

Note: This is a basic tutorial meaning we are using all predefined function libraries and no extended classes with exception handling. It doesn't offer the complete in-depth analysis you should acquire before attempting to build a stand alone PHP based FTP system. However, it will allow you to create a basic interaction system for rare occassions.

In this tutorial we will look at the built in FTP functions that PHP versions 4 and 5 offer us. Note that most of these functions can be found in PHP versions 3 3.0.13 and above except for ftp_close() which requires version 4.2.0.

In this tutorial we will:
- Connect to an FTP server
- Login to an FTP server
- Retrieve a single file
- Upload a single file
- Retrieve a list of files in a directory

Let's start off by connecting to the server. You should have your FTP host setup in the function ftp_connect() like so:

$resource = ftp_connect('ftp.host.com');

This will establish a connection to the host and let you know if it even exists. If that host cannot be found, an error will be returned.

The connection and login process is very simple and self explanitory. The login function is ftp_login() and is used like so:

$login = ftp_login($resource, 'username', 'password');

You'll notice that we have to call the original FTP resource locator. You should become comfortable with doing this, since most if not all FTP functions require an FTP resource to be executed.

If you have an FTP server setup and you know your connection info you shouldn't have gotten any errors at this point. Now it's time to get into the useful features of PHP's FTP interaction system. First, let's retrieve a list of files from a specific directory.

$list = ftp_rawlist($resource, '/mydir');

This will return a raw list of all the files in a directory. Depending on the operating system, a series of information could be returned for each file name. If you want to check the OS of the FTP server you can use the ftp_systype() function; however, since this is a basic tutorial we are not going to worry about that and we're going to continue listing files without specific interpretation.

However, for our exampe we're going to assume we're using a UNIX operating system. For each file you should receive a bunch of code similar to this piece:
rw-r--r-- 1 32009 user_account 6435 Feb 19 20:19 filename.ext

There are three important aspects of that returned code. The first being the type of the "file" being returned, the second the modified date, and the last the actual file name.

If you notice the first file name prefix, rw-r--r-r--, you can probably guess it has special meaning. Well, it does. For a little information about what these things mean you can check out http://nik.seet.com/ftp.html. However, this doesn't offer a very comprehensive insight so you may need to do more research if you want to get into directory/file distinguishing.

Next is the modified date, it's pretty self explanitory and I think you all can figure out which piece of code that is. Lastly is the file name, this is useful because, of course, it's the actual file name on the server.

If you're becoming comfortable with php you might want to try an explode() or preg_split() on the file information to make it cleaner to read.

Now that we have a list of files it's time to retrieve them and see their contents. For this we'll use the ftp_get() function. Let's look at an example:

$file = ftp_get($resource, 'tmp.txt', '/dir/myfile.ext', FTP_BINARY);

You'll notice we called the FTP resource stream stored in $resource again. Next is the file on the local server where the file contents should be stored. A simple .txt will do in most cases; however, you should chmod it accordingly so the entire world can't see the contents even if you're erasing them after the script is run. The next argument is the remote file path, this should be the path to the file on the FTP server that you would like to retrieve. The final argument is the transfer mode. For the transfer mode we're going to use a standard FTP_BINARY mode rather than the FTP_ASCII transfer mode. These are the only two transfer modes in the php function library so you may want to read more on the uses of each.

To view the contents of the file transferred from FTP you'd have to echo the file contents of the local file you specified in the ftp_get() function.

Next we're going to put a file on the FTP server. It's very similar to the ftp_get() function and it's called ftp_put(). Let's check it out:

$putFile = ftp_put($resource, '/dir/myuploadedfile.ext', 'filetoupload.ext', FTP_BINARY);

Again we use the resource as the first argument. The second argument is the path on the FTP server in which the file should be uploaded. Thirdly is the local file on the PHP server that is being uploaded. The last required argument is the transfer mode again. Read above for a bit more information.

When you're all said and done with the ftp interaction in your script you should make a call to ftp_close() if you're running a PHP version of 4.2.0 or higher. In most cases, all hosts have this or a later version installed already.

The ftp_close() function takes one argument, the FTP resource. We'd call it like this:

ftp_close($resource);

Okay, that's it! If you digested this perfectly then you could be ready to start interacting with FTP servers via PHP. I will create a more in-depth tutorial later next week about building classes and using OOP to help make the experience more customized and easier to navigate.

While FTP isn't nearly as fast as retrieving file contents from a local server, it can offer much more functionality in interacting with servers besides your own.

Good night.


None

Claxor

Reply To Post Reply & Quote

Posted at: 3/20/06 10:01 AM

Claxor DARK LEVEL 10

Sign-Up: 10/21/05

Posts: 2,465

Sweet tutorial :D
Although I don't see any personal use for that, in my case right now, it should come in handy some time :).

BBS Signature

None

Jams44

Reply To Post Reply & Quote

Posted at: 3/20/06 11:34 PM

Jams44 LIGHT LEVEL 07

Sign-Up: 11/08/04

Posts: 719

another great tutorial, thnx PD


None

Jordan

Reply To Post Reply & Quote

Posted at: 7/19/06 04:51 PM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

nice, this could probably be usefull when i start coding a portal :D


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/19/06 04:55 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/19/06 04:51 PM, doodle_flash wrote: nice, this could probably be usefull when i start coding a portal :D

You wouldn't really want to use these functions for coding a portal....

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

DFox

Reply To Post Reply & Quote

Posted at: 7/19/06 04:57 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,463

At 7/19/06 04:55 PM, SpamBurger wrote: You wouldn't really want to use these functions for coding a portal....

You might if you want to FTP the submitted files to a dedicated server just to host submitted content.

I would guess this is what Newgrounds does.


None

SpamBurgerPie

Reply To Post Reply & Quote

Posted at: 7/20/06 09:30 AM

SpamBurgerPie EVIL LEVEL 03

Sign-Up: 07/20/06

Posts: 123

At 7/19/06 04:57 PM, GamesCool wrote:
At 7/19/06 04:55 PM, SpamBurger wrote: You wouldn't really want to use these functions for coding a portal....
You might if you want to FTP the submitted files to a dedicated server just to host submitted content.

I would guess this is what Newgrounds does.

Yep, this is probably one of the best ways to do it if you need a dedicated ftp server to handle all of the uploads/downloads.


All times are Eastern Standard Time (GMT -5) | Current Time: 06:55 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!