PHP的FTP学习(四)


Posted in PHP onOctober 09, 2006

By Vikram Vaswani
Melonfire
November 07, 2000
以下是代码列表:
--------------------------------------------------------------------------------
<!-- 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 相关文章推荐
我的论坛源代码(四)
Oct 09 PHP
Adodb的十个实例(清晰版)
Dec 31 PHP
PHP用GD库生成高质量的缩略图片
Mar 09 PHP
php中取得文件的后缀名?
Feb 20 PHP
YII模块实现绑定二级域名的方法
Jul 09 PHP
PHP制作图形验证码代码分享
Oct 23 PHP
PHP获取毫秒级时间戳的方法
Apr 15 PHP
PHP模板引擎Smarty自定义变量调解器用法
Apr 11 PHP
详解Yii2.0使用AR联表查询实例
Jun 16 PHP
Laravel实现短信注册的示例代码
May 29 PHP
Ubuntu彻底删除PHP7.0的方法
Jul 27 PHP
laravel框架语言包拓展实现方法分析
Nov 22 PHP
杏林同学录(七)
Oct 09 #PHP
一个连接两个不同MYSQL数据库的PHP程序
Oct 09 #PHP
我的论坛源代码(一)
Oct 09 #PHP
我的论坛源代码(二)
Oct 09 #PHP
我的论坛源代码(三)
Oct 09 #PHP
我的论坛源代码(四)
Oct 09 #PHP
PHP的FTP学习(三)
Oct 09 #PHP
You might like
用PHP读取RSS feed的代码
2008/08/01 PHP
php中使用preg_match_all匹配文章中的图片
2013/02/06 PHP
php笔记之:php数组相关函数的使用
2013/04/26 PHP
php批量删除cookie的简单实现方法
2015/01/26 PHP
php将图片文件转换成二进制输出的方法
2015/06/10 PHP
PHP操作Postgresql封装类与应用完整实例
2018/04/24 PHP
laravel中的一些简单实用功能
2018/11/03 PHP
PHP序列化的四种实现方法与横向对比
2018/11/29 PHP
PHP微商城开源代码实例
2019/03/27 PHP
url地址自动加#号问题说明
2010/08/21 Javascript
整理8个很棒的 jQuery 倒计时插件和教程
2011/12/12 Javascript
JavaScript中prototype为对象添加属性的误区介绍
2013/10/15 Javascript
iframe子页面获取父页面元素的方法
2013/11/05 Javascript
原生js的数组除重复简单实例
2016/05/24 Javascript
javascript 解决浏览器不支持的问题
2016/09/24 Javascript
Bootstrap基本样式学习笔记之标签(5)
2016/12/07 Javascript
详解webpack-dev-server使用http-proxy解决跨域问题
2018/01/13 Javascript
微信小程序实现验证码获取倒计时效果
2018/02/08 Javascript
微信小程序实现上传图片功能
2018/05/28 Javascript
Vue实现textarea固定输入行数与添加下划线样式的思路详解
2018/06/28 Javascript
javascript执行上下文、变量对象实例分析
2020/04/25 Javascript
antd的select下拉框因为数据量太大造成卡顿的解决方式
2020/10/31 Javascript
[01:15:45]DOTA2上海特级锦标赛B组小组赛#1 Alliance VS Spirit第一局
2016/02/26 DOTA
[05:49]DOTA2-DPC中国联赛 正赛 Elephant vs LBZS 选手采访
2021/03/11 DOTA
理解Python中函数的参数
2015/04/27 Python
Python 稀疏矩阵-sparse 存储和转换
2017/05/27 Python
Python使用django搭建web开发环境
2017/06/09 Python
python实现梯度下降算法
2020/03/24 Python
jupyter notebook 的工作空间设置操作
2020/04/20 Python
Python是如何进行类型转换的
2013/06/09 面试题
酒吧总经理岗位职责
2013/12/10 职场文书
教师年度考核自我鉴定
2014/01/19 职场文书
餐饮业员工工作决心书
2014/03/11 职场文书
手术室护士长竞聘书
2014/03/31 职场文书
python 标准库原理与用法详解之os.path篇
2021/10/24 Python
Java实现学生管理系统(IO版)
2022/02/24 Java/Android