Php ftp Learning (3)

Source: Internet
Author: User
Tags ftp connection ftp site

By Vikram Vaswani
Melonfire
November 07,200 0
Now, we have been familiar with a large number of FTP functions in PHP, but this is just a function, far from our goal. To show the real power of these functions, we should establish Program This program can upload and download files on the web --- this is what we will do!

When we enterCodePreviously, I would like to tell you that this example is only intended to explain to you how to use various FTP functions of PHP. It is not complete in many aspects, such as error analysis, as for how you want to apply it to your own program, you should make some modifications!

The program includes the following files:
Index.html-Logon File

Actions. php-FTP Code required by the program

Include. php-main program interface, which displays the file list and control buttons.

Let's start with "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>
<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, and password. Enter a box. The input variables are saved to the $ server, $ username, and $ password variables. After the form is submitted, actions. php is called, which initializes the FTP connection.

Note that "hidden" is a variable $ action passed to action. php and the value is CWD.

This is the source code of the action. php file:
--------------------------------------------------------------------------------
<HTML>
<Head>
<Basefont face = Arial>
</Head>
<Body>

<! -- The include. php interface will be inserted into this page -->

<?

// Check the data transmitted from the form. If the data is incomplete, an error is returned. To improve the program, a more comprehensive input detection function should be provided here.
If (! $ Server |! $ Username |! $ Password)
{
Echo "incomplete data submission! ";
}
Else
{
// Keep reading
}

?>

</Body>
</Html>
--------------------------------------------------------------------------------

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

"Action = CWD"

Change working directory

"Action = Delete"

Delete a specified file

"Action = download"

Download a specified file

"Action = upload"

Upload a specified file

If you carefully check the file include. PHP, including an HTML interface, you will see that it includes many forms, each pointing to a specific function, each form contains a field (usually hidden), when the form is submitted, the corresponding function will be executed.

For example, if you press "Delete" and "Action = Delete", the message is sent to "actions. php"

The code in actions. php is as follows:
--------------------------------------------------------------------------------
<?
// Action: Change the Directory
If ($ action = "CWD ")
{
// Code
}

// Action: delete an object
Else if ($ action = "delete ")
{
// Code
}
// Action: download an object
Else if ($ action = "Download ")
{
// Code
}
// Action: upload a file
Else if ($ action = "Upload ")
{
// Code
}

?>
--------------------------------------------------------------------------------
The specific code above will implement the specified function and exit the loop. They all include the following steps:

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

Turn to the appropriate directory

Execute the selected Function

Refresh the list to view the changed results

Use include ("include. php") to display the file list and control buttons

Close connection
--------------------------------------------------------------------------------
Note:
The following functions support multi-file operations-"Action = Delete" and "Action = download", which are implemented using the for loop.
Variables $ cdir and $ here are updated in real time at each stage.

Now we finally get to our third file, include. php, which creates a user interface for the program.

"Include. php" contains three forms. Some PHP code obtains the current directory list and stores them in three variables.
$ Files (including files in the current directory ),
$ File_sizes (corresponding file size ),
And $ dirs (including subdirectory name)

The first form generates a drop-down directory list using $ dirs, corresponding to "Action = CWD ".

The second form uses $ files $ file_sizes to create an available file list. Each file uses a checkbox. The action of this form corresponds to "Action = Delete" and "Action = download"

the third form is used to upload a file to the FTP site, as follows:
upload


...

...

Accept
when PHP receives a file name in this way, some variables are generated, which specify the file size, A temporary file name and file type. The initial file name contains $ upfile_name. Once uploaded, the file name is saved to $ upfile (this variable is created by PHP)
with this information, we can create the following statement:
usage
ftp_put ($ result, $ upfile_name, $ upfile, ftp_binary );
--------------------------------------------------------------------------------

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.