PHP的FTP学习(三)
Melonfire
November 07, 2000
现在,我们已经接触了php关于FTP的大量函数,但这仅仅只是函数,离我们的目标还远远不够,要显示出这些函数的真正力量,我们应该建立一个程序,这个程序能以WEB方式上传,下载文件---这就是我们将要做的!
在我们进入代码前,我想要告诉大家的是,这个例子仅仅只是为了向大家解释PHP的各种FTP函数的使用,很多方面还不够完善,比如说,错误分析等,至于你想应用到你自己的程序中,你应该进行一些修改!
程序包括以下几个文件:
index.html - 登录文件
actions.php - 程序必需的FTP代码
include.php - 程序主界面,它显示文件列表和控制按钮。
让我们从 "index.html"开始吧:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
这是一个登录表单,有一个服务器名称、用户名、密码,输入框。输入的变量将会被存到$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);
--------------------------------------------------------------------------------

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
