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 相关文章推荐
模拟flock实现文件锁定
Feb 14 PHP
PHP递归返回值时出现的问题解决办法
Feb 19 PHP
php实现memcache缓存示例讲解
Dec 04 PHP
PHP抓取、分析国内视频网站的视频信息工具类
Apr 02 PHP
ThinkPHP模版中导入CSS和JS文件的方法
Nov 29 PHP
PHP实现过滤各种HTML标签
May 17 PHP
php实现常见图片格式的水印和缩略图制作(面向对象)
Jun 15 PHP
Apache无法自动跳转却显示目录的解决方法
Nov 30 PHP
php 截取utf-8格式的字符串实例代码
Oct 30 PHP
[原创]php token使用与验证示例【测试可用】
Aug 30 PHP
php微信公众号开发之秒杀
Oct 20 PHP
PHP大文件分块上传功能实例详解
Jul 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 删除无限级目录与文件代码共享
2008/11/22 PHP
php备份数据库类分享
2015/04/14 PHP
jQuery 处理表单元素的代码
2010/02/15 Javascript
javascript 判断整数方法分享
2014/12/16 Javascript
js面向对象之静态方法和静态属性实例分析
2015/01/10 Javascript
Express实现前端后端通信上传图片之存储数据库(mysql)傻瓜式教程(一)
2015/12/10 Javascript
教你如何终止JQUERY的$.AJAX请求
2016/02/23 Javascript
JavaScript File分段上传
2016/03/10 Javascript
jQuery与JS加载事件用法分析
2016/09/04 Javascript
react-native-tab-navigator组件的基本使用示例代码
2017/09/07 Javascript
Vue通过URL传参如何控制全局console.log的开关详解
2017/12/07 Javascript
Vue组件之单向数据流的解决方法
2018/11/10 Javascript
JavaScript 空间坐标的使用
2020/08/19 Javascript
解决vue项目打包上服务器显示404错误,本地没出错的问题
2020/11/03 Javascript
原生js 实现表单验证功能
2021/02/08 Javascript
vue中axios封装使用的完整教程
2021/03/03 Vue.js
python代码 输入数字使其反向输出的方法
2018/12/22 Python
Python 正则表达式匹配字符串中的http链接方法
2018/12/25 Python
Python3.4学习笔记之列表、数组操作示例
2019/03/01 Python
python中dict使用方法详解
2019/07/17 Python
python采集百度搜索结果带有特定URL的链接代码实例
2019/08/30 Python
Python控制鼠标键盘代码实例
2020/12/08 Python
Python实现钉钉/企业微信自动打卡的示例代码
2021/02/02 Python
css3实现波纹特效、H5实现动态波浪效果
2018/01/31 HTML / CSS
CSS3 实现童年的纸飞机
2019/05/05 HTML / CSS
html5使用html2canvas实现浏览器截图的示例
2017/08/31 HTML / CSS
 Alo Yoga官网:购买瑜伽服装
2018/06/17 全球购物
软件测试面试题
2014/01/05 面试题
党校学习思想汇报
2014/01/06 职场文书
庆中秋节主题活动方案
2014/02/03 职场文书
计算机售后服务承诺书
2014/05/30 职场文书
离婚协议书范文2014
2014/10/16 职场文书
机关班子查摆问题及整改措施
2014/10/28 职场文书
党的群众路线教育实践活动总结材料
2014/10/30 职场文书
听证通知书
2015/04/24 职场文书
小学作文指导之如何写人?
2019/07/08 职场文书