By Vikram Vaswani
Melonfire
November 07, 2000
Now, we have come into contact with a large number of functions related to FTP in PHP, but these are just functions and are far from our goal. To show the real power of these functions, we should Create a program that can upload and download files via WEB --- this is what we will do!
Before we get into the code, what I want to tell you is that this example is just to explain the use of various FTP functions of PHP. It is not perfect in many aspects, such as error analysis, etc. As for what you want to apply it to In your own program, you should make some modifications!
The program includes the following files:
index.html - login file
actions.php - FTP code necessary for the program
include.php - the main interface of the program, which displays the file list and control buttons.
Let’s start with "index.html":
---------------------------------------- ---------------------------------------------
---------------- -------------------------------------------------- -------------
This is a login form with a server name, username, password, and input box. The entered variables will be stored in the $server, $username and $password variables. After the form is submitted, actions.php is called, which will initialize the FTP connection.
Pay attention to the "hidden" which passes a variable $action to action.php with the value CWD.
This is the source code of the action.php file:
--------------------------------------------- -------------------------------------
//Check form If the transmitted data is incomplete, an error will be reported. If you want the program to be perfect, there should be a more complete input detection function here
if (!$server || !$username || !$password)
{
echo "The submitted data is incomplete! ";
}
else
{
// keep reading
}
?>
----------------- -------------------------------------------------- ------------
Next is the variable "actions". The program allows the following actions:
"action=CWD"
Change the working directory
"action=Delete"
Delete the specified file
"action =Download"
Download the specified file
"action=Upload"
Upload the specified file
If you carefully examine the file include.php, which contains 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: when "Delete" is pressed, "action=Delete" is sent to "actions.php"
In order to operate these four functions, the code in actions.php is as follows:
---------- -------------------------------------------------- -------------------
// action: change directory
if ($action == "CWD")
{
// specific code
}
// action: Delete file
else if ($action == "Delete")
{
// Specific code
}
// action: Download file
else if ($action == "Download")
{
// Specific code
}
// action: Upload file
else if ($action == "Upload")
{
// Specific code
}
?>
---------- -------------------------------------------------- -------------------
The above specific codes will implement the specified functions and exit the loop. They all include the following steps:
-------- -------------------------------------------------- -----------------------
Connect and log in to the FTP server through a customized function
connect();
Shift to the appropriate directory
Perform the selected function
Refresh the list , to see the result of the change
Display the file list and control buttons through include("include.php")
Close the connection
----------------------- -------------------------------------------------- -------
Note:
The following functions support multi-file operations - namely "action=Delete" and "action=Download" which are implemented using FOR loops.
Variables $cdir and $here will be updated in real time at each stage.
Now we finally get to our third file, include.php which sets up a user interface for the program.
"include.php" contains three forms, some PHP code to get the current directory list and store them into three variables
$files (including files in the current directory),
$file_sizes (corresponding file sizes),
and $dirs (contains subdirectory names)
The first form uses $dirs to generate a drop-down directory list, corresponding to "action=CWD".
The second form uses $files $file_sizes to create a list of available files, and 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:
---------------- -------------------------------------------------- --------------
----------------------------------------- ------------------------------------------
When PHP receives this way When a file name is given, some variables are generated. These variables specify the file size, a temporary file name and the file type. The initial file name is stored in $upfile_name. Once uploaded, the file name is stored in $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);
-------------------------------- ---------------------------------------------
The above introduces PHP FTP learning three, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.