PHP的FTP学习(二)[转自奥索]


Posted in PHP onOctober 09, 2006

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

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

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

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

第三个表单用来上传一个文件到FTP站点,如下:
--------------------------------------------------------------------------------
<form enctype="multipart/form-data" action=actions.php4 method=post>
...
<input type=file name=upfile>
...
</form>
--------------------------------------------------------------------------------
当PHP以这种方式接收到一个文件名,一些变量就产生了,这些变量指定文件的大小,一个临时的文件名以及文件的类型,最初的文件名存在$upfile_name,一旦上传后文件名便存入$upfile中(这个变量是由PHP自己创建的)
通过这些信息,我们就可以创建以下的语句了:
--------------------------------------------------------------------------------
ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
--------------------------------------------------------------------------------
以下是代码列表:
--------------------------------------------------------------------------------
<!-- code for index.html begins here -->
<html>
<head>
<basefont face=arial>
</head>

<body>

<table border=0 align=center>
<form action="actions.php" method=post>
<input type=hidden name=action value=CWD>
<tr>
<td>
Server
</td>
<td>
<input type=text name=server>
</td>
</tr>

<tr>
<td>
User
</td>
<td>
<input type=text name=username>
</td>
</tr>

<tr>
<td>
Password
</td>
<td>
<input type=password name=password>
</td>
</tr>

<tr>
<td colspan=2 align=center>
<input type="submit" value="Beam Me Up, Scotty!">
</td>
</tr>

</form>
</table>

</body>
</html>

<!-- code for index.html ends here -->
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<!-- code for actions.php begins here -->

<html>
<head>
<basefont face=Arial>
</head>
<body>

<?
/*
--------------------------------------------------------------------------------
DISCLAIMER:

This is use-at-your-own-risk code.

It is meant only for illustrative purposes and is not meant for production environments. No warranties of any kind are provided to the user.

You have been warned!

All code copyright Melonfire, 2000. Visit us at http://www.melonfire.com
--------------------------------------------------------------------------------
*/

// function to connect to FTP server
function connect()
{
global $server, $username, $password;
$conn = ftp_connect($server);
ftp_login($conn, $username, $password);
return $conn;
}

// main program begins

// check for valid form entries else print error
if (!$server
!$username
!$password)
{
echo "Form data incomplete!";
}
else
{

// connect
$result = connect();

// action: change directory
if ($action == "CWD")
{

// at initial stage $rdir does not exist
// so assume default directory
if (!$rdir)
{
$path = ".";
}
// get current location $cdir and add it to requested directory $rdir
else
{
$path = $cdir . "/" . $rdir;
}

// change to requested directory
ftp_chdir($result, $path);

}

// action: delete file(s)
else if ($action == "Delete")
{

ftp_chdir($result, $cdir);

// loop through selected files and delete
for ($x=0; $x<sizeof($dfile); $x++)
{
ftp_delete($result, $cdir . "/" . $dfile[$x]);
}

}
// action: download files
else if ($action == "Download")
{

ftp_chdir($result, $cdir);

// download selected files
// IMPORTANT: you should specify a different download location here!!
for ($x=0; $x<sizeof($dfile); $x++)
{
ftp_get($result, $dfile[$x], $dfile[$x], FTP_BINARY);
}

}
// action: upload file
else if ($action == "Upload")
{

ftp_chdir($result, $cdir);

// put file

/*
a better idea would be to use
$res_code = ftp_put($result, $HTTP_POST_FILES["upfile"]["name"],
$HTTP_POST_FILES["upfile"]["tmp_name"], FTP_BINARY);
as it offers greater security
*/
$res_code = ftp_put($result, $upfile_name, $upfile, FTP_BINARY);

// check status and display
if ($res_code == 1)
{
$status = "Upload successful!";
}
else
{
$status = "Upload error!";
}

}

// create file list
$filelist = ftp_nlist($result, ".");

// and display interface
include("include.php");

// close connection
ftp_quit($result);

}
?>

</body>
</html>

<!-- code for actions.php ends here -->
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<!-- code for include.php begins here -->

<?

// get current location
$here = ftp_pwd($result);

/*
since ftp_size() is quite slow, especially when working
on an array containing all the files in a directory,
this section performs an ftp_size() on all the files in the current
directory and creates three arrays.
*/

// array for files
$files = Array();

// array for directories
$dirs = Array();

// array for file sizes
$file_sizes = Array();

// counters
$file_list_counter = 0;
$dir_list_counter = 0;

// check each element of $filelist
for ($x=0; $x<sizeof($filelist); $x++)
{
if (ftp_size($result, $filelist[$x]) != -1)
{
// create arrays
$files[$file_list_counter] = $filelist[$x];
$file_sizes[$file_list_counter] = ftp_size($result, $filelist[$x]);
$file_list_counter++;
}
else
{
$dir_list[$dir_list_counter] = $filelist[$x];
$dir_list_counter++;
}
}

?>

<!-- header - where am I? -->
<center>
You are currently working in <b><? echo $here; ?></b>
<br>
<!-- status message for upload function -->
<? echo $status; ?>
</center>
<hr>
<p>
<!-- directory listing in drop-down list -->
Available directories:
<form action=actions.php method=post>

<!-- these values are passed hidden every time -->
<!-- a more optimal solution might be to place these in session
variables -->
<input type=hidden name=username value=<? echo $username; ?>>
<input type=hidden name=password value=<? echo $password; ?>>
<input type=hidden name=server value=<? echo $server; ?>>
<input type=hidden name=cdir value=<? echo $here; ?>>

<!-- action to take when THIS form is submitted -->
<input type=hidden name=action value=CWD>

<!-- dir listing begins; first item is for parent dir -->
<select name=rdir>
<option value=".."><parent directory></option>

<?

for ($x=0; $x<sizeof($dir_list); $x++)
{
echo "<option value=" . $dir_list[$x] . ">" . $dir_list[$x] . "</option>";

}
?>

</select>
<input type=submit value=Go>
</form>

<hr>

<!-- file listing begins -->

Available files:
<form action=actions.php method=post>

<!-- these values are passed hidden every time -->
<input type=hidden name=server value=<? echo $server; ?>>
<input type=hidden name=username value=<? echo $username; ?>>
<input type=hidden name=password value=<? echo $password; ?>>
<input type=hidden name=cdir value=<? echo $here; ?>>

<table border=0 width=100%>

<?

// display file listing with checkboxes and sizes
for ($y=0; $y<sizeof($files); $y++)
{
echo "<tr><td><input type=checkbox name=dfile[] value=" . $files[$y] .
">". $files[$y] . " <i>(" . $file_sizes[$y] . " bytes)</i><td>";
}

?>
</table>

<!-- actions for this form -->
<center>
<input type=submit name=action value=Delete>
<input type=submit name=action value=Download>
</center>
</form>
<p>

<hr>

<!-- file upload form -->
File upload:
<form enctype="multipart/form-data" action=actions.php method=post>

<!-- these values are passed hidden every time -->
<input type=hidden name=username value=<? echo $username; ?>>
<input type=hidden name=password value=<? echo $password; ?>>
<input type=hidden name=server value=<? echo $server; ?>>
<input type=hidden name=cdir value=<? echo $here; ?>>

<table>
<tr>
<td>
<!-- file selection box -->
<input type=file name=upfile>
</td>
</tr>
<tr>
<td>
<!-- action for this form -->
<input type=submit name=action value=Upload>
</td>
</tr>
</table>
</form>

<!-- code for include.php ends here -->

PHP 相关文章推荐
给初学PHP的5个入手程序
Nov 23 PHP
windows下开发并编译PHP扩展的方法
Mar 18 PHP
解析php中call_user_func_array的作用
Jun 07 PHP
基于PHP+Ajax实现表单验证的详解
Jun 25 PHP
实例讲解yii2.0在php命令行中运行的步骤
Dec 01 PHP
PHP Echo字符串的连接格式
Mar 07 PHP
Yii2.0 模态弹出框+ajax提交表单
May 22 PHP
php使用ftp实现文件上传与下载功能
Jul 21 PHP
PHP实现基于栈的后缀表达式求值功能
Nov 10 PHP
tp5(thinkPHP5框架)时间查询操作实例分析
May 29 PHP
Laravel框架实现的上传图片到七牛功能详解
Sep 06 PHP
Laravel框架Eloquent ORM修改数据操作示例
Dec 03 PHP
在PHP中利用XML技术构造远程服务(上)
Oct 09 #PHP
在PHP中利用XML技术构造远程服务(下)
Oct 09 #PHP
把从SQL中取出的数据转化成XMl格式
Oct 09 #PHP
JAVA/JSP学习系列之四
Oct 09 #PHP
JAVA/JSP学习系列之二
Oct 09 #PHP
递归列出所有文件和目录
Oct 09 #PHP
不用iconv库的gb2312与utf-8的互换函数
Oct 09 #PHP
You might like
PHP扩展编写点滴 技巧收集
2010/03/09 PHP
国外PHP程序员的13个好习惯小结
2012/02/20 PHP
如何获知PHP程序占用多少内存(memory_get_usage)
2012/09/23 PHP
Codeigniter注册登录代码示例
2014/06/12 PHP
php表单敏感字符过滤类
2014/12/08 PHP
PHP实现简单数字分页效果
2015/07/26 PHP
PHP内核探索之变量
2015/12/22 PHP
php微信公众平台示例代码分析(二)
2016/12/06 PHP
yii2.0框架数据库操作简单示例【添加,修改,删除,查询,打印等】
2020/04/13 PHP
firefox下jquery iframe刷新页面提示会导致重复之前动作
2012/12/17 Javascript
Table冻结表头示例代码
2013/08/20 Javascript
jquery easyui 对于开始时间小于结束时间的判断示例
2014/03/22 Javascript
javascript实现保留两位小数的多种方法
2015/12/18 Javascript
深入理解Angular2 模板语法
2016/08/07 Javascript
Vue.js实现一个todo-list的上移下移删除功能
2017/06/26 Javascript
BootStrap自定义popover,点击区域隐藏功能的实现
2018/01/23 Javascript
详解vue文件中使用echarts.js的两种方式
2018/10/18 Javascript
JS数组splice操作实例分析
2019/10/12 Javascript
JS实现导航栏楼层特效
2020/01/01 Javascript
JS实现网站楼层导航效果代码实例
2020/06/16 Javascript
Vue 构造选项 - 进阶使用说明
2020/08/14 Javascript
Python  __getattr__与__setattr__使用方法
2008/09/06 Python
python正则实现计算器功能
2017/12/14 Python
python实现与redis交互操作详解
2020/04/21 Python
如何在Python对Excel进行读取
2020/06/04 Python
Python importlib模块重载使用方法详解
2020/10/13 Python
全球领先美式家具品牌:Ashley爱室丽家居
2017/08/07 全球购物
官方授权图形T恤和服装:Fifth Sun
2019/06/12 全球购物
乌克兰最大的家用电器和电子产品连锁店:Eldorado
2019/10/02 全球购物
怎样声明一个匿名的内部类
2016/06/01 面试题
医学生毕业自我鉴定
2014/03/26 职场文书
运动会拉拉队口号
2014/06/09 职场文书
检讨书模板大全
2015/05/07 职场文书
HTML+CSS制作心跳特效的实现
2021/05/26 HTML / CSS
python中 .npy文件的读写操作实例
2022/04/14 Python
Python如何利用pandas读取csv数据并绘图
2022/07/07 Python