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文件向另一个地址post数据,不用表单和隐藏的变量的
Mar 06 PHP
php 数组的指针操作实现代码
Feb 08 PHP
如何利用PHP执行.SQL文件
Jul 05 PHP
PHP扩展CURL的用法详解
Jun 20 PHP
php输出金字塔的2种实现方法
Dec 16 PHP
PHP使用PDO连接ACCESS数据库
Mar 05 PHP
深入浅出php socket编程
May 13 PHP
php session 写入数据库
Feb 13 PHP
Linux下快速搭建php开发环境
Mar 13 PHP
解决Laravel 不能创建 migration 的问题
Oct 09 PHP
在Laravel中使用GuzzleHttp调用第三方服务的API接口代码
Oct 15 PHP
Yii使用EasyWechat实现小程序获取用户的openID的方法
Apr 29 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中支持多种编码的中文字符串截取函数!
2007/03/20 PHP
Phpbean路由转发的php代码
2008/01/10 PHP
由php的call_user_func传reference引发的思考
2010/07/23 PHP
PHP动态创建Web站点的方法
2011/08/14 PHP
PHP中HTML标签过滤技巧
2014/01/07 PHP
PHP采集类Snoopy抓取图片实例
2014/06/19 PHP
php使用Jpgraph绘制柱形图的方法
2015/06/10 PHP
分享10段PHP常用代码
2015/11/11 PHP
PHP基于简单递归函数求一个数阶乘的方法示例
2017/04/26 PHP
网页自动跳转代码收集
2009/09/27 Javascript
工作中常用到的JS表单验证代码(包括例子)
2010/11/11 Javascript
修改好的jquery滚动字幕效果实现代码
2011/06/22 Javascript
40个有创意的jQuery图片、内容滑动及弹出插件收藏集之一
2011/12/31 Javascript
js的隐含参数(arguments,callee,caller)使用方法
2014/01/28 Javascript
浅析JavaScript回调函数应用
2016/05/22 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
Koa2微信公众号开发之消息管理
2018/05/16 Javascript
js中console在一行内打印字符串和对象的方法
2019/09/10 Javascript
JS实现电商商品展示放大镜特效
2020/01/07 Javascript
vue动态加载SVG文件并修改节点数据的操作代码
2020/08/17 Javascript
[01:00:30]完美世界DOTA2联赛循环赛 Inki vs Matador BO2第二场 10.31
2020/11/02 DOTA
python利用标准库如何获取本地IP示例详解
2017/11/01 Python
详解Pandas之容易让人混淆的行选择和列选择
2019/07/10 Python
win10下安装Anaconda的教程(python环境+jupyter_notebook)
2019/10/23 Python
tensorflow 实现数据类型转换
2020/02/17 Python
python 解决tqdm模块不能单行显示的问题
2020/02/19 Python
浅谈python opencv对图像颜色通道进行加减操作溢出
2020/06/03 Python
在线吉他课程,学习如何弹吉他:Fender Play
2019/02/28 全球购物
2014年党务公开实施方案
2014/02/27 职场文书
幼儿园秋游感想
2014/03/12 职场文书
产品陈列协议书(标准版)
2014/09/17 职场文书
2015年中秋节演讲稿
2015/03/20 职场文书
刑事附带民事诉讼答辩状
2015/05/22 职场文书
读书笔记怎么写
2015/07/01 职场文书
地震捐款简报
2015/07/21 职场文书
2016年社会管理综治宣传月活动总结
2016/03/16 职场文书