FTP Learning in PHP (i) [Osso]_php Foundation

Source: Internet
Author: User
Tags ftp connection joins parent directory zip

by Vikram Vaswani
Melonfire
November 07, 2000
We are a group of PHP fans, we use it for a variety of reasons-web site development, drawing, database connectivity, etc.-we found that it is very friendly, powerful and easy to use ...
You may have seen how PHP was used to create GIF and JPEG images, get information dynamically from the database, and so on, but this is just the tip of the iceberg---the latest version of PHP has a powerful file transfer capability.
In this tutorial, I'll show you how FTP transmits files via HTTP and FTP connections, and there are some simple program codes to follow.

First, you should know that PHP transmits files through HTTP and FTP joins. Uploading files over HTTP has already appeared in PHP3, and now the new FTP function has appeared in the new PHP version!
Before you start, you need to be sure that your PHP supports FTP, and you can use the following code to find out:

--------------------------------------------------------------------------------
?

Phpinfo ();

?>
--------------------------------------------------------------------------------
Check the output, there is a "Additional Modules" area, here is a list of your PHP-supported modules; If you don't find the FTP module, you'd better reinstall PHP and add FTP support!

Let's take a look at how a typical FTP task is done!
--------------------------------------------------------------------------------
$ ftp ftp.server.com
Connected to Ftp.server.com
server.com FTP server ready.
Name (Server:john): John
331 Password required for John.
Password:
230 User John logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
Ftp> ls
PORT command successful.
Opening ASCII mode data connection For/bin/ls.
Drwxr-xr-x 5 John users 3072 Nov 2 11:03.
Drwxr-xr-x the root root 2048 Nov 1 23:26.
drwxr--r--2 John users 1024 Oct 5 13:26 Bin
Drwx--x--x 8 John users 1024 Nov 2 10:59 public_html
drwxr--r--4 John users 1024 Nov 2 11:26 tmp
-rw-r--r--1 John users 2941465 Oct 9 17:21 data.zip
Transfer complete.
Ftp> bin
Type set to I.
Ftp> Get Data.zip
Local:data.zip Remote:data.zip
PORT command successful.
Opening BINARY mode data connection for Data.zip (2941465 bytes).
Transfer complete.
Ftp> bye
221 Goodbye.
--------------------------------------------------------------------------------
As you can see, the process is clearly divided into several segments: joins (a connection to the FTP server), authentication (determining whether the user has the power to enter the system), transmission (this includes the column directory, uploading or downloading files), and canceling the join.

To use PHP for FTP
Creating a PHP FTP join must follow the basic steps of opening a join-issuing authentication information-manipulating directories and transferring files using PHP functions.
The following specific implementation:
--------------------------------------------------------------------------------
?

Join an FTP server
$conn = Ftp_connect ("ftp.server.com");

Log on using username and password
Ftp_login ($conn, "John", "Doe");

Get remote system type
Ftp_systype ($conn);

Listing files
$filelist = Ftp_nlist ($conn, ".");

Download files
Ftp_get ($conn, "Data.zip", "Data.zip", ftp_binary);

Close Join
Ftp_quit ($conn);

?>
--------------------------------------------------------------------------------
Let's take this step-by-step:
To initialize an FTP join, PHP provides the Ftp_connect () function, which uses the host name and port as parameters. In the example above, the host name is "Ftp.server.com", and if the port is not specified, PHP will use "21" as the default port to establish the join.
Ftp_connect () returns a handle handle after the join succeeds, and this handle will be used by the FTP function used later.
--------------------------------------------------------------------------------
?

Connect to FTP Server
$conn = Ftp_connect ("ftp.server.com");

?>
--------------------------------------------------------------------------------
Once a join is established, use Ftp_login () to send a user name and user password. As you can see, this function Ftp_login () uses the handle from the Ftp_connect () function to determine that the user name and password can be committed to the correct server.
--------------------------------------------------------------------------------
?

Log in with username and password
Ftp_login ($conn, "John", "Doe");

?>
--------------------------------------------------------------------------------
At this point, you can do what you want to do, specifically in the next section:

After doing what you want to do, remember to use the Ftp_quit () function to turn off your FTP connection

--------------------------------------------------------------------------------
?

Close connection
Ftp_quit ($conn);

?>
--------------------------------------------------------------------------------
Logged on to the FTP server, PHP provides functions that can get information about systems and files and directories.

Ftp_pwd ()
If you want to know your current directory, you need to use this function.
--------------------------------------------------------------------------------
?

Get Current Location
$here = Ftp_pwd ($conn);

?>
--------------------------------------------------------------------------------
What if you need to know what system is running on the server side?
Ftp_systype () provides you with this information.
--------------------------------------------------------------------------------
?

Get system Type
$server _os = Ftp_systype ($conn);

?>
--------------------------------------------------------------------------------
On the passive mode (PASV) switch, PHP also provides such a function, it can turn on or off PASV (1 means open)
--------------------------------------------------------------------------------
?

Turn PASV on
FTP_PASV ($conn, 1);

?>
--------------------------------------------------------------------------------

Now that you know where you are and who is with you, now we're going to go around the table of contents--The Ftp_chdir () function, which takes a directory name as an argument.
--------------------------------------------------------------------------------
?

Change directory to "Public_html"
Ftp_chdir ($conn, "public_html");

?>
--------------------------------------------------------------------------------
If you want to go back to the directory you're in (the parent directory), Ftp_cdup () can help you achieve your wish, and this function can go back to the previous directory.
--------------------------------------------------------------------------------
?

Go up one level in the directory tree
Ftp_cdup ($conn);

?>
--------------------------------------------------------------------------------
You can also create or move a directory, using the Ftp_mkdir () and Ftp_rmdir () functions; note: if Ftp_mkdir () is successful, it will return the newly created directory name.
--------------------------------------------------------------------------------
?

Make the directory "test"
Ftp_mkdir ($conn, "test");

Remove the directory "test"
Ftp_rmdir ($conn, "test");

?>
--------------------------------------------------------------------------------
Creating an FTP directory is usually the transfer of files---so let's get started!

First upload file, Ftp_put () function is very good for this responsibility, it requires you to specify a local file name, uploaded file name and the type of transmission. For example: If you want to upload "abc.txt" This file, after uploading the name "Xyz.txt", the order should be this:
--------------------------------------------------------------------------------
?

Upload
Ftp_put ($conn, "Xyz.txt", "Abc.txt", ftp_ascii);

?>
--------------------------------------------------------------------------------
Download file:
PHP provides the function is Ftp_get (), it also requires a server file name, download the file name, as well as transport type as parameters, such as: Server-side files for His.zip, you want to download to the local machine, and named Hers.zip, the command is as follows:
--------------------------------------------------------------------------------
?

Download
Ftp_get ($conn, "Hers.zip", "His.zip", ftp_binary);

?>
--------------------------------------------------------------------------------
PHP defines two modes as transmission mode Ftp_binary and FTP_ASCII, the use of these two models to see two cases, as for its detailed explanation, this article also said, please refer to the relevant books.




How can I make a list of documents? (with Dir?) :) )
PHP offers two methods: one is to simply list the file name and directory, and the other is to specify the file size, permissions, time of creation, and so on.
The first type uses the Ftp_nlist () function, and the second uses the Ftp_rawlist (). Two functions require a directory name as a parameter, which returns a catalog column as an array, each of which corresponds to a row of the list.
--------------------------------------------------------------------------------
?

Obtain file Listing
$filelist = Ftp_nlist ($conn, ".");

?>
--------------------------------------------------------------------------------
You must want to know the size of the file! Don't worry, there's a very easy function ftp_size (), which returns the size of the file you specified, using bites as your unit. To point out, if it returns "-1", it means that this is a directory, and in the following example you will see the application of this function.
--------------------------------------------------------------------------------
?

Obtain file size of file "Data.zip"
$filelist = Ftp_size ($conn, "data.zip");

?>

Now, we have contacted PHP about FTP a large number of functions, but this is just a function, far from our goal is not enough, to show the true power of these functions, we should establish a program, this program can be uploaded to the web, download files---this is what we will do!

Before we enter the code, I want to tell you that this example is just to explain the use of PHP's various FTP functions, many aspects are not perfect, for example, error analysis, as you want to apply to your own program, you should make some changes!

The procedure includes the following documents:
Index.html-Login File

actions.php-Required FTP code for a program

include.php-Program main interface, which displays a list of files and control buttons.

Let's start with the "index.html":
--------------------------------------------------------------------------------
<table border=0 align=center>
<form action= "actions.php" method=post>
<input Type=hidden name=action value=cwd>
<tr>
<td>
Server
</td>
<td>
<input Type=text name=server>
</td>
</tr>

<tr>
<td>
User
</td>
<td>
<input Type=text name=username>
</td>
</tr>

<tr>
<td>
Password
</td>
<td>
<input Type=password name=password>
</td>
</tr>

<tr>
&LT;TD colspan=2 align=center>
<input type= "Submit" value= "Beam Me up, scotty!" >
</td>
</tr>

</form>
</table>
--------------------------------------------------------------------------------
This is a login form, with a server name, user name, password, input box. The variables entered will be saved to the $server, $username and $password variables, the form is submitted, the actions.php is invoked, and it initializes the FTP join.

Notice that "hidden" it passes to action.php a variable $action with a value of CWD.


This is the source code for the action.php file:
--------------------------------------------------------------------------------
<basefont face=arial>
<body>

<!--the include.php interface'll be inserted to this page-->

?

Check the data from the form, not all the error, to improve the program, there should be more full input detection function
if (! $server
! $username
! $password)
{
echo "Submit data incomplete!";
}
Else
{
Keep reading
}

?>

</body>
--------------------------------------------------------------------------------


Next is the variable "actions". The program allows the following action:

"ACTION=CWD"

Change Working directory

"Action=delete"

Delete the specified file

"Action=download"

Download the specified file

"Action=upload"

Upload specified file

If you examine the file include.php and include an HTML interface in it, you will see that it includes many forms, each pointing to a specific feature, each containing a field (usually hidden), and when the form is submitted, the corresponding function is executed.

For example: Press "delete", "Action=delete" will be sent to "actions.php"

To operate these four functions, the code in actions.php is as follows:
--------------------------------------------------------------------------------
?
Action: Changing directory
if ($action = = "CWD")
{
Specific code
}

Action: Deleting files
else if ($action = = "Delete")
{
Specific code
}
Action: Downloading files
else if ($action = = "Download")
{
Specific code
}
Action: Uploading Files
else if ($action = = "Upload")
{
Specific code
}

?>
--------------------------------------------------------------------------------
The specific code above will implement the specified functionality and exit the loop, which all contain the following steps:

--------------------------------------------------------------------------------
Join and log on to the FTP server through custom functions
Connect ();

Turn to the appropriate directory

Perform the selected function

Refresh the list to see the result of the change

Display file lists and control buttons with include ("include.php")

Close Join
--------------------------------------------------------------------------------
Attention:
The following features support multiple file operations-that is, "Action=delete" and "Action=download", which are implemented using a for loop.
Variable $cdir and $here will be updated in real time at each stage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.