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 相关文章推荐
php生成zip压缩文件的方法详解
Jun 09 PHP
php实现快速排序的三种方法分享
Mar 12 PHP
php选择排序法实现数组排序实例分析
Feb 16 PHP
php实现httpRequest的方法
Mar 13 PHP
Nginx下配置codeigniter框架方法
Apr 07 PHP
php生成图片验证码的实例讲解
Aug 03 PHP
PHP使用php-resque库配合Redis实现MQ消息队列的教程
Jun 29 PHP
示例详解Laravel重置密码代码重构
Aug 10 PHP
php生成无限栏目树
Mar 16 PHP
php头像上传预览实例代码
May 02 PHP
Laravel下生成验证码的类
Nov 15 PHP
Yii框架连表查询操作示例
Sep 06 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
jQuery EasyUI datagrid实现本地分页的方法
2015/02/13 Javascript
JQuery工具函数汇总
2015/06/15 Javascript
js弹出对话框方式小结
2015/11/17 Javascript
浅谈Jquery中Ajax异步请求中的async参数的作用
2016/06/06 Javascript
浅谈jquery点击label触发2次的问题
2016/06/12 Javascript
jQuery特殊符号转义的实现
2016/11/30 Javascript
MvcPager分页控件 适用于Bootstrap
2017/06/03 Javascript
动态创建Angular组件实现popup弹窗功能
2017/09/15 Javascript
Node.js的Koa实现JWT用户认证方法
2018/05/05 Javascript
vue实现微信分享功能
2018/11/28 Javascript
javascript导出csv文件(excel)的方法示例
2019/08/25 Javascript
[40:10]2015国际邀请赛全明星表演赛
2015/08/07 DOTA
[44:22]完美世界DOTA2联赛循环赛 FTD vs PXG BO2第一场 11.01
2020/11/02 DOTA
[47:52]DOTA2-DPC中国联赛正赛 iG vs LBZS BO3 第二场 3月4日
2021/03/11 DOTA
Python中的zip函数使用示例
2015/01/29 Python
Python卸载模块的方法汇总
2016/06/07 Python
浅谈python对象数据的读写权限
2016/09/12 Python
利用Python进行异常值分析实例代码
2017/12/07 Python
Python中pandas模块DataFrame创建方法示例
2018/06/20 Python
对Python3+gdal 读取tiff格式数据的实例讲解
2018/12/04 Python
使用python的pandas为你的股票绘制趋势图
2019/06/26 Python
Python实现性能自动化测试竟然如此简单
2019/07/30 Python
Django之使用内置函数和celery发邮件的方法示例
2019/09/16 Python
如何安装2019Pycharm最新版本(详细教程)
2019/09/26 Python
对python中 math模块下 atan 和 atan2的区别详解
2020/01/17 Python
StubHub巴西:购买和出售您的门票
2016/07/22 全球购物
新西兰廉价汽车租赁:Snap Rentals
2018/09/14 全球购物
办加油卡单位介绍信
2014/01/09 职场文书
加拿大探亲邀请信
2014/01/28 职场文书
大学毕业感言100字
2014/02/03 职场文书
岗位职责风险点
2014/03/12 职场文书
大学生励志演讲稿
2014/04/25 职场文书
海南召开党的群众路线教育实践活动总结大会新闻稿
2014/10/21 职场文书
2019入党申请书格式和范文
2019/06/25 职场文书
2019年七夕情人节浪漫祝福语大全!
2019/08/08 职场文书
关于Spring配置文件加载方式变化引发的异常详解
2022/01/18 Java/Android