Home Backend Development PHP Tutorial PHP的FTP学习(三)

PHP的FTP学习(三)

Jun 01, 2016 pm 02:30 PM

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 of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1676
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP and Frameworks: Modernizing the Language PHP and Frameworks: Modernizing the Language Apr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles