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编程与应用
Oct 09 PHP
PHP无限分类代码,支持数组格式化、直接输出菜单两种方式
May 18 PHP
web目录下不应该存在多余的程序(安全考虑)
May 09 PHP
PHP应用JSON技巧讲解
Feb 03 PHP
PHP生成自适应大小的缩略图类及使用方法分享
May 06 PHP
php里array_work用法实例分析
Jul 13 PHP
yii2.0实现创建简单widgets示例
Jul 18 PHP
php实现在新浪云中使用imagick生成缩略图并上传的方法
Sep 26 PHP
CI框架数据库查询缓存优化的方法
Nov 21 PHP
Laravel模型事件的实现原理详解
Mar 14 PHP
Laravel自定义 封装便捷返回Json数据格式的引用方法
Sep 29 PHP
laravel-admin 在列表页添加自定义按钮的例子
Sep 30 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
咖啡是不是喝了会上瘾?咖啡是必须品吗!
2021/03/04 新手入门
PHP的FTP学习(四)
2006/10/09 PHP
PHP语法速查表
2006/12/06 PHP
php中隐形字符65279(utf-8的BOM头)问题
2014/08/16 PHP
为你的网站增加亮点的9款jQuery插件推荐
2011/05/03 Javascript
jQuery当鼠标悬停时放大图片的效果实例
2013/07/03 Javascript
input标签内容改变的触发事件介绍
2014/06/18 Javascript
在JavaScript中操作时间之setYear()方法的使用
2015/06/12 Javascript
jQuery toggle 代替方法
2016/03/22 Javascript
JS获取checkbox的个数简单实例
2016/08/19 Javascript
如何使用vuejs实现更好的Form validation?
2017/04/07 Javascript
jQuery实现jQuery-form.js实现异步上传文件
2017/04/28 jQuery
深入浅析Node.js单线程模型
2017/07/10 Javascript
解决vue里碰到 $refs 的问题的方法
2017/07/13 Javascript
vue树形结构获取键值的方法示例
2018/06/21 Javascript
jQuery模拟12306城市选择框功能简单实现方法示例
2018/08/13 jQuery
谈谈为什么你的 JavaScript 代码如此冗长
2019/01/30 Javascript
详解vue.js移动端配置flexible.js及注意事项
2019/04/10 Javascript
vue实现密码显示与隐藏按钮的自定义组件功能
2019/04/23 Javascript
vue项目打包后怎样优雅的解决跨域
2019/05/26 Javascript
vue中的过滤器实例代码详解
2019/06/06 Javascript
浅谈Vue项目骨架屏注入实践
2019/08/05 Javascript
微信小程序实现购物车小功能
2020/12/30 Javascript
python操作CouchDB的方法
2014/10/08 Python
python使用fileinput模块实现逐行读取文件的方法
2015/04/29 Python
python pandas读取csv后,获取列标签的方法
2018/11/12 Python
完美解决keras保存好的model不能成功加载问题
2020/06/11 Python
Python就将所有的英文单词首字母变成大写
2021/02/12 Python
办公室内勤岗位职责范本
2013/12/09 职场文书
工作会议欢迎词
2014/01/16 职场文书
网吧消防安全制度
2014/01/28 职场文书
企业安全生产演讲稿
2014/05/09 职场文书
企业党员一句话承诺
2014/05/30 职场文书
小学趣味运动会加油稿
2014/09/25 职场文书
初中班主任心得体会
2016/01/07 职场文书
创业计划书之干洗店
2019/09/10 职场文书