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 csv操作类代码
Dec 14 PHP
php设计模式 Adapter(适配器模式)
Jun 26 PHP
php适配器模式介绍
Aug 14 PHP
配置php网页显示各种语法错误
Sep 23 PHP
CodeIgniter框架中_remap()使用方法2例
Mar 10 PHP
ThinkPHP关于session的操作方法汇总
Jul 18 PHP
php简单实现多维数组排序的方法
Sep 30 PHP
php技巧小结【推荐】
Jan 19 PHP
微信公众平台开发-微信服务器IP接口实例(含源码)
Mar 05 PHP
PHP实践教程之过滤、验证、转义与密码详解
Jul 24 PHP
PHP实现的日历功能示例
Sep 01 PHP
php面向对象基础详解【星际争霸游戏案例】
Jan 23 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
2020年4月放送!《Princess Connect Re:Dive》制作组 & 角色声优公开!
2020/03/06 日漫
dhtmlxTree目录树增加右键菜单以及拖拽排序的实现方法
2013/04/26 PHP
分享PHP header函数使用教程
2013/09/05 PHP
php中mysql操作buffer用法详解
2015/03/19 PHP
PHP微信开发之文本自动回复
2016/06/23 PHP
基于jquery的图片懒加载js
2010/06/30 Javascript
javascript算法学习(直接插入排序)
2011/04/12 Javascript
基于JQuery模仿苹果桌面的Dock效果(初级版)
2012/10/15 Javascript
js onload处理html页面加载之后的事件
2013/10/30 Javascript
使用js dom和jquery分别实现简单增删改
2014/09/11 Javascript
基于BootStrap环境写jQuery tabs插件
2016/07/12 Javascript
JavaScript中的对象和原型(一)
2016/08/12 Javascript
3种不同的ContextMenu右键菜单实现代码
2016/11/03 Javascript
jQuery实现发送验证码并60秒倒计时功能
2016/11/25 Javascript
简单谈谈vue的过渡动画(推荐)
2017/10/11 Javascript
jQuery实现的手动拖动控制进度条效果示例【测试可用】
2018/04/18 jQuery
React学习之受控组件与数据共享实例分析
2020/01/06 Javascript
解决Vue的项目使用Element ui 走马灯无法实现的问题
2020/08/03 Javascript
[51:29]Alliance vs TNC 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
Python中使用PIPE操作Linux管道
2015/02/04 Python
python中利用Future对象回调别的函数示例代码
2017/09/07 Python
对Python 文件夹遍历和文件查找的实例讲解
2018/04/26 Python
Python 普通最小二乘法(OLS)进行多项式拟合的方法
2018/12/29 Python
Python 利用pydub库操作音频文件的方法
2019/01/09 Python
Python3实现的简单三级菜单功能示例
2019/03/12 Python
python五子棋游戏的设计与实现
2019/06/18 Python
python绘制已知点的坐标的直线实例
2019/07/04 Python
Python实现一个带权无回置随机抽选函数的方法
2019/07/24 Python
python装饰器实现对异常代码出现进行自动监控的实现方法
2020/09/15 Python
基于Python中Remove函数的用法讨论
2020/12/11 Python
Boden澳大利亚官网:英国在线服装公司
2018/08/05 全球购物
小学中等生评语
2014/12/29 职场文书
农民工工资支付承诺书
2015/05/04 职场文书
我的中国梦心得体会范文
2016/01/05 职场文书
MySQL中CURRENT_TIMESTAMP的使用方式
2021/11/27 MySQL
Oracle 11g数据库使用expdp每周进行数据备份并上传到备份服务器
2022/06/28 Oracle