FTP Learning in PHP (iii)

Source: Internet
Author: User
Keywords FTP Learning in PHP (iii)
Tags ftp site
by Vikram Vaswani
Melonfire
November 07, 2000
Now, we have been exposed to PHP a large number of FTP functions, but this is only a function, far from our goal is not enough, to show the true power of these functions, we should establish a program, the program can be uploaded web-based, 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, etc., as you want to apply to your own program, you should make some changes!

The program consists of several files:
Index.html-Login File

actions.php-Required FTP code for the program

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

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



--------------------------------------------------------------------------------
This is a login form that has a server name, user name, password, and input box. The input variables will be stored in the $server, $username and $password variables, and after the form is committed, call actions.php, which initializes the FTP join.

Notice that "hidden" it passed to action.php a variable $action, with a value of CWD.


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









Check the data from the form, not all the error, in order to improve the program, there should be a more complete input detection function
if (! $server | |! $username | |! $password)
{
echo "Submit data not complete!";
}
Else
{
Keep reading
}

?>



--------------------------------------------------------------------------------


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

"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 and include an HTML interface inside, you will see that it includes many forms, each pointing to a specific function, each containing a field (usually hidden), and when the form is submitted, the corresponding function will be executed.

For example: Press "delete", "Action=delete" is sent to "actions.php"

In order to operate these four functions, the code in actions.php is as follows:
--------------------------------------------------------------------------------
Action: Changing the Directory
if ($action = = "CWD")
{
Specific code
}

Action: Deleting files
else if ($action = = "Delete")
{
Specific code
}
Action: Download file
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, both of which contain the following steps:

--------------------------------------------------------------------------------
Join and log into the FTP server via custom functions
Connect ();

Move to the appropriate directory

Perform the selected function

Refresh the list to see the results of the changes

Show file list and control buttons via include ("include.php")

Close Join
--------------------------------------------------------------------------------
Attention:
The following features support multi-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 get to our third file, include.php it establishes a user interface for the program.

"Include.php" contains three forms, and some PHP code gets the current list of directories and stores them in three variables
$files (including files in the current directory),
$file _sizes (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 of which uses a checkbox. The action for 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 file name in this way, some variables are generated, these variables specify the size of the file, a temporary file name, and the type of files, the original file name exists $upfile_name, once the file name is uploaded into the $ Upfile (this variable was created by PHP itself)
With this information, we can create the following statements:
--------------------------------------------------------------------------------
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.