Home  >  Article  >  Backend Development  >  PHP FTP learning (3)_PHP tutorial

PHP FTP learning (3)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:03:49716browse

By Vikram Vaswani
Melonfire
November 07, 2000
Now, we have been exposed to a large number of PHP functions related to FTP, but these are just functions and are far from our goal. To display these functions For the real power, we should build 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 far as you want to apply it to your own programs, 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 - Main interface of the program , which displays a file list and control buttons.

Let’s start with "index.html":
----------------------------- --------------------------------------------------




< ;tr>



















Server



User



Password






-------------------------- -------------------------------------------------- --
This is a login form with a server name, username, password, and input boxes. 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.

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


This is the source code of the action.php file:
----------------------------- -------------------------------------------------- -









//Check the data sent from the form, if it is not complete, 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 "Incomplete data submitted!";
}
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 check the file include.php carefully, include an HTML in it The interface, as you will see, consists of many forms, each pointing to a specific function. Each form contains a field (usually hidden), and 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 code will implement the specified function and exit the loop , they all contain the following steps:

---------------------------------------- ---------------------------------------------
Through customized functions Connect and log in to the FTP server
connect();

Change to the appropriate directory

Execute the selected function

Refresh the list to see the results of the changes

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.
The 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 (containing 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 a filename in this way, a number of variables are generated that specify the size of the file, a temporary filename, and the file type. Initially The file name exists 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);
--- -------------------------------------------------- --------------------------

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316094.htmlTechArticleBy Vikram Vaswani Melonfire November 07, 2000 Now, we have been exposed to a large number of PHP functions for FTP, but this is just It’s just a function, which is far from our goal. We need to show this...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn