FTP Learning in PHP (iii)

Source: Internet
Author: User
by Vikram Vaswani
Melonfire
November 07, 2000
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.

And now we finally got to our third file, include.php it creates a user interface for the program.

"Include.php" contains three forms, some PHP code gets the current directory list and deposits them into three variables
$files (including files in the current directory),
$file _sizes (the corresponding file size),
and $dirs (contains subdirectory names)

The first form uses $dirs to produce a drop-down list of directories, corresponding to "ACTION=CWD".

The second form uses $files $file _sizes to create a list of available files, each using 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:
--------------------------------------------------------------------------------
<form enctype= "Multipart/form-data" Action=actions.php4 method=post>
...
<input Type=file name=upfile>
...
</form>
--------------------------------------------------------------------------------
When PHP receives a filename in this way, some variables are generated, these variables specify the size of the file, a temporary file name and the type of file, the original filename exists $upfile_name, once uploaded, the filename is deposited into $ Upfile (this variable is created by PHP itself)
With this information, we can create the following statement:
--------------------------------------------------------------------------------
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.