ByVikramVaswaniMelonfireNovember07, 2000 now, we are already in touch with a large number of php FTP functions, but this is just a function, far from our goal, and we need to show the real power of these functions, we should establish a program that can upload and download files on the WEB --- this is what we will do! Before entering the code, I would like to tell you that this example is only By Vikram Vaswani
Melonfire
November 07,200 0
Now, we have come into contact with a large number of FTP functions in php, but this is just a function. it is far from our goal and we need to show the real strength of these functions, we should establish a program that can upload and download files on the WEB --- this is what we will do!
Before entering the code, I would like to tell you that this example is only intended to explain the use of various FTP functions in PHP, and is not complete in many aspects. for example, error analysis and so on. 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:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
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:
--------------------------------------------------------------------------------
// 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
}
?>
--------------------------------------------------------------------------------
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 shown below:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
When PHP receives a file name in this way, some variables are generated. these variables specify the file size, a temporary file name, and the 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 statements:
--------------------------------------------------------------------------------
Ftp_put ($ result, $ upfile_name, $ upfile, FTP_BINARY );
--------------------------------------------------------------------------------