search

By Vikram Vaswani
Melonfire
November 07, 2000
现在,我们已经接触了php关于FTP的大量函数,但这仅仅只是函数,离我们的目标还远远不够,要显示出这些函数的真正力量,我们应该建立一个程序,这个程序能以WEB方式上传,下载文件---这就是我们将要做的!

在我们进入代码前,我想要告诉大家的是,这个例子仅仅只是为了向大家解释PHP的各种FTP函数的使用,很多方面还不够完善,比如说,错误分析等,至于你想应用到你自己的程序中,你应该进行一些修改!

程序包括以下几个文件:
index.html - 登录文件

actions.php - 程序必需的FTP代码

include.php - 程序主界面,它显示文件列表和控制按钮。

让我们从 "index.html"开始吧: 
--------------------------------------------------------------------------------
























Server



User



PassWord





--------------------------------------------------------------------------------
这是一个登录表单,有一个服务器名称、用户名、密码,输入框。输入的变量将会被存到$server, $username 和 $password 变量中,表单提交后,调用actions.php,它将初始化FTP联接。

注意那个“hidden” 它传给action.php一个变量$action ,值为CWD。


这是action.php文件的源码:
--------------------------------------------------------------------------------










//检查表单传来的数据,不全则报错,要想程序完善的话,这里应该有更全的输入检测功能
if (!$server || !$username || !$password)
{
echo "提交数据不全!";
}
else
{
// keep reading
}

?>



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


接下来是变量 "actions". 程序允许以下的action:

"action=CWD"

改变工作目录

"action=Delete"

删除指定文件

"action=Download"

下载指定文件

"action=Upload"

上传指定文件

如果你仔细检查文件include.php,在里面包括一个HTML界面,你将会看到,它包括许多表单,每一个指向一个特定的功能,每一个表单包含一个field(通常隐藏) ,当表单提交,相应的功能将被执行。

例如:按下“删除”,"action=Delete"就被传送给"actions.php" 

为了操作这四个功能,actions.php中代码如下:  
--------------------------------------------------------------------------------

// action: 改变目录
if ($action == "CWD")
{
// 具体代码
}

// action: 删除文件
else if ($action == "Delete")
{
// 具体代码
}
// action: 下载文件
else if ($action == "Download")
{
// 具体代码
}
// action: 上传文件
else if ($action == "Upload")
{
// 具体代码
}

?>
--------------------------------------------------------------------------------
以上的具体代码将会实现指定的功能,并退出循环,它们都包含以下步骤:

--------------------------------------------------------------------------------
通过定制的函数联接并登录FTP服务器
connect();

转向适当的目录

执行选择的功能

刷新列表,以察看改变的结果

通过include("include.php"),显示文件列表和控制按钮

关闭联接
--------------------------------------------------------------------------------
注意:
以下功能支持多文件操作- 即 "action=Delete" 和 "action=Download" 它们使用FOR循环来实现。
变量$cdir 和 $here 将在每一阶段实时更新。

现在终于到了我们的第三个文件,include.php 它为程序建立起一个用户界面。

"include.php" 包含三个表单,一些PHP代码获取当前的目录列表并将它们存入三个变量
$files (包括当前目录下的文件),
$file_sizes (相应的文件大小),
and $dirs (包含子目录名)

第一个表单使用$dirs 产生一个下拉式目录列表,对应于“action=CWD”。

第二个表单使用$files $file_sizes创建一个可用的文件列表,每一个文件使用一个checkbox。这个表单的action对应于"action=Delete" and "action=Download"

第三个表单用来上传一个文件到FTP站点,如下:
--------------------------------------------------------------------------------

...

...

--------------------------------------------------------------------------------
当PHP以这种方式接收到一个文件名,一些变量就产生了,这些变量指定文件的大小,一个临时的文件名以及文件的类型,最初的文件名存在$upfile_name,一旦上传后文件名便存入$upfile中(这个变量是由PHP自己创建的)
通过这些信息,我们就可以创建以下的语句了:
--------------------------------------------------------------------------------
ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
--------------------------------------------------------------------------------

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
How to calculate the total number of elements in a PHP multidimensional array?How to calculate the total number of elements in a PHP multidimensional array?May 15, 2025 pm 09:00 PM

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

What are the characteristics of do-while loops in PHP?What are the characteristics of do-while loops in PHP?May 15, 2025 pm 08:57 PM

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

How to hash strings in PHP?How to hash strings in PHP?May 15, 2025 pm 08:54 PM

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

How to implement array sliding window in PHP?How to implement array sliding window in PHP?May 15, 2025 pm 08:51 PM

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

How to use the __clone method in PHP?How to use the __clone method in PHP?May 15, 2025 pm 08:48 PM

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

How to use goto statements in PHP?How to use goto statements in PHP?May 15, 2025 pm 08:45 PM

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

How to implement data statistics in PHP?How to implement data statistics in PHP?May 15, 2025 pm 08:42 PM

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

How to use anonymous functions in PHP?How to use anonymous functions in PHP?May 15, 2025 pm 08:39 PM

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values ​​of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment