web方式ftp


Posted in PHP onOctober 09, 2006

<?php

/* $Id: ftp.php,v 2.2 2002/03/12 23:21:28 pauls Exp $ */

/* This software came from http://inebria.com/ */

/* Copyright (c) 2000
      Paul Southworth.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote
    products derived from this software without specific prior
    written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

/* Configuration Options */

$phpftp_host="localhost";
$phpftp_version="2.2";

/* Comment this out if you don't want the version footer */

$show_version_footer=1;

/* How large a file will you accept?  You may also need to edit your
   php.ini file and change upload_max_filesize appropriately */

$max_file_size="1000000";

/* The temporary directory $phpftp_tmpdir must exist and be writable
   by your web server.
   Hint:  mkdir /var/tmp/xfers && chmod 1777 /var/tmp/xfers */

$phpftp_tmpdir="/var/tmp/xfers";

/* $use_mime_lookup
   Turning this on creates a dependency upon the
   http://www.inebria.com/mime_lookup/ MIME type lookup library.
   Setting this variable to "1" enables it.  "0" disables.
   If you turn it on, put the mime_lookup.php file in the same directory
   as ftp.php and uncomment the 'include("mime_lookup.php");' statement. */

$use_mime_lookup="0";
/* include("mime_lookup.php"); */

/* We enclose the top and bottom in functions because sometimes
   we might not send them (ie, in a file-download situation) */

function phpftp_top() {
    global $phpftp_version;
?>
<!-- function phpftp_top -->
<html>
<head>
<title>PHP FTP Client <?php echo $phpftp_version; ?></title>
</head>
<body bgcolor="#ffffff">
<?php
}

function phpftp_bottom() {
    global $phpftp_version;
    global $show_version_footer;
?>
<!-- function phpftp_bottom -->
<?php
if (isset($show_version_footer)) {
?>
<p><font size=-2>This is <a href="http://inebria.com/phpftp/">PHP FTP</a>
version <?php echo $phpftp_version; ?></font></p>
<?php
}
?>
</body>
</html>
<?php
}

/* This is the form used for initially collecting username/passwd */

function phpftp_login() {
    phpftp_top();
?>
<!-- function phpftp_login -->
<p>
<form action="ftp.php" method=post>
<p><table border=0>
<tr><td>
Login:
</td><td>
<input name="phpftp_user" type="text">
</td></tr>
<tr><td>
Password:
</td><td>
<input name="phpftp_passwd" type="password">
</td></tr>
<tr><td>
Directory:
</td><td>
<input name="phpftp_dir" type="text">
</td></tr>
</table>
</p><p>
<input type="hidden" name="function" value="dir">
<input type="submit" value="connect">
</p>
</form>
<p>
<?php
    phpftp_bottom();
}

/* This function does not return TRUE/FALSE - it returns the value of
   $ftp, the current FTP stream. */

function phpftp_connect($phpftp_user,$phpftp_passwd) {
    global $phpftp_host;
    $ftp = ftp_connect($phpftp_host);
    if ($ftp) {
        if (ftp_login($ftp,$phpftp_user,urldecode($phpftp_passwd))) {
            return $ftp;
        }
    }
}

function phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir) {
    global $phpftp_host;
    phpftp_top();
?>
<!-- function phpftp_list -->
<?php
    $ftp = @phpftp_connect($phpftp_user,$phpftp_passwd);
    if (!$ftp) {
?>
<strong>FTP login failed!</strong>
<a href="ftp.php">Start over?</a>
<?php
        phpftp_bottom();
    } else {
        if (!$phpftp_dir) {
            $phpftp_dir=ftp_pwd($ftp);
        }
        if (!@ftp_chdir($ftp,$phpftp_dir)) {
?>
<font color="#ff0000"><strong>Can't enter that directory!</strong></font><p><p>
<?php
            $phpftp_dir=ftp_pwd($ftp);
        }
        echo "<strong>Current host:</strong> " . $phpftp_host . "<br>\n";
        echo "<strong>Current directory:</strong> " . $phpftp_dir . "<br>\n";
        if ($phpftp_dir == "/") {
            $phpftp_dir="";
        }

        if ($contents = ftp_rawlist($ftp,"")) {
            $d_i=0;
            $f_i=0;
            $l_i=0;
            $i=0;
            while ($contents[$i]) {
                $item[] = split("[ ]+",$contents[$i],9);
                $item_type=substr($item[$i][0],0,1);
                if ($item_type == "d") {
                    /* it's a directory */
                    $nlist_dirs[$d_i]=$item[$i][8];
                    $d_i++;
                } elseif ($item_type == "l") {
                    /* it's a symlink */
                    $nlist_links[$l_i]=$item[$i][8];
                    $l_i++;
                } elseif ($item_type == "-") {
                    /* it's a file */
                    $nlist_files[$f_i]=$item[$i][8];
                    $nlist_filesize[$f_i]=$item[$i][4];
                    $f_i++;
                } elseif ($item_type == "+") {
                    /* it's something on an anonftp server */
                    $eplf=split(",",implode(" ",$item[$i]),5);
                    if ($eplf[2] == "r") {
                        /* it's a file */
                        $nlist_files[$f_i]=trim($eplf[4]);
                        $nlist_filesize[$f_i]=substr($eplf[3],1);
                        $f_i++;
                    } elseif ($eplf[2] == "/") {
                        /* it's a directory */
                        $nlist_dirs[$d_i]=trim($eplf[3]);
                        $d_i++;
                    }
                } /* ignore all others */
                $i++;
            }
?>
<table border=0 cellspacing=20>
<?php
            if (count($nlist_dirs)>0) {
?>
<tr><td align=left valign=top>
<strong>Directories</strong><br>
<form action="ftp.php" method=post>
<input type="hidden" name="function" value="cd">
<input type="hidden" name="phpftp_user" value="<?php echo $phpftp_user; ?>">
<input type="hidden" name="phpftp_passwd" value="<?php echo $phpftp_passwd; ?>">
<input type="hidden" name="phpftp_dir" value="<?php echo $phpftp_dir; ?>">
<select name="select_directory" size="10" width="100">
<?php
                for ($i=0; $i < count($nlist_dirs); $i++) {
                    echo "<option value=\"" . $nlist_dirs[$i] . "\">" . $nlist_dirs[$i] . "</option>\n";
                }
?>
</select><br>
<input type="submit" value="Enter Directory">
</form>
</td>
<?php
            }

            if (count($nlist_files)>0) {
?>
<td align=left valign=top>
<strong>Files</strong><br>
<form action="ftp.php" method=post>
<input type="hidden" name="function" value="get">
<input type="hidden" name="phpftp_user" value="<?php echo $phpftp_user; ?>">
<input type="hidden" name="phpftp_passwd" value="<?php echo $phpftp_passwd; ?>">
<input type="hidden" name="phpftp_dir" value="<?php echo $phpftp_dir; ?>">
<select name="select_file" size="10">
<?php
                for ($i=0; $i < count($nlist_files); $i++) {
                    echo "<option value=\"" . $nlist_files[$i] . "\">" . $nlist_files[$i] ."  ($nlist_filesize[$i] bytes)". "</option>\n";
                }
?>
</select><br>
<input type="submit" value="Download File">
</form>
</td></tr>
<?php
            }
        } else {
?>
<p><font color="#ff0000"><strong>Directory empty or not readable</strong></font><p>
<?php
        }
?>
</table>
<p>
<form action="ftp.php" method=post>
<?php
        $cdup=dirname($phpftp_dir);
        if ($cdup == "") {
            $cdup="/";
        }
?>
<input type="hidden" name="function" value="dir">
<input type="hidden" name="phpftp_user" value="<?php echo $phpftp_user; ?>">
<input type="hidden" name="phpftp_passwd" value="<?php echo $phpftp_passwd; ?>">
<input type="hidden" name="phpftp_dir" value="<?php echo $cdup; ?>">
<input type="submit" value="Go up one directory">
</form>
<p>
<form enctype="multipart/form-data" action="ftp.php" method=post>
<input type="hidden" name="max_file_size" value="<?php echo $max_file_size ?>">
<input type="hidden" name="phpftp_user" value="<?php echo $phpftp_user; ?>">
<input type="hidden" name="phpftp_passwd" value="<?php echo $phpftp_passwd; ?>">
<input type="hidden" name="phpftp_dir" value="<?php echo $phpftp_dir; ?>">
<input type="hidden" name="function" value="put">
<input type="submit" value="Upload this:">
<input name="userfile" type="file">
</form>
<p>
<form action="ftp.php" method=post>
<input type="hidden" name="function" value="mkdir">
<input type="hidden" name="phpftp_user" value="<?php echo $phpftp_user; ?>">
<input type="hidden" name="phpftp_passwd" value="<?php echo $phpftp_passwd; ?>">
<input type="hidden" name="phpftp_dir" value="<?php echo $phpftp_dir; ?>">
<input type="submit" value="Make subdirectory:">
<input name="new_dir" type="text">
<?php
        ftp_quit($ftp);
        phpftp_bottom();
    }
}

function phpftp_cd($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_directory) {
?>
<!-- function phpftp_cd -->
<?php
    $new_directory=$phpftp_dir . "/" . $select_directory;
    phpftp_list($phpftp_user,$phpftp_passwd,$new_directory);
}

function phpftp_mkdir($phpftp_user,$phpftp_passwd,$phpftp_dir,$new_dir) {
?>
<!-- function phpftp_mkdir -->
<?php
    $ftp = @phpftp_connect($phpftp_user,$phpftp_passwd);
    if ($phpftp_dir == "") {
        $phpftp_dir="/";
    }
    if (!$ftp) {
        @ftp_quit($ftp);
        phpftp_top();
?>
<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>
<a href="ftp.php">Start over?</a>
<?php
        phpftp_bottom();
    } else {
        $dir_path = $phpftp_dir . "/" . $new_dir;
        @ftp_mkdir($ftp,$dir_path);
        @ftp_quit($ftp);
        phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir);
    }
};

function phpftp_get($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_file) {
    $ftp = @phpftp_connect($phpftp_user,$phpftp_passwd);
    if ($phpftp_dir == "") {
        $phpftp_dir="/";
    }
    if ((!$ftp) || (!@ftp_chdir($ftp,$phpftp_dir))) {
        @ftp_quit($ftp);
        phpftp_top();
?>
<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>
<a href="ftp.php">Start over?</a>
<?php
        phpftp_bottom();
    } else {
        srand((double)microtime()*1000000);
        $randval = rand();
        $tmpfile=$phpftp_tmpdir . "/" . $select_file . "." . $randval;
        if (!ftp_get($ftp,$tmpfile,$select_file,FTP_BINARY)) {
            ftp_quit($ftp);
            phpftp_top();
?>
<font color="#ff0000"><strong>FTP get failed!</strong></font><p><p>
<a href="ftp.php">Start over?</a>
<?php
            phpftp_bottom();
        } else {
            ftp_quit($ftp);
            global $use_mime_lookup;
            if ($use_mime_lookup == "1") {
                $file_mime_type=mime_lookup(substr(strrchr($select_file,"."),1));
            }
            if (!$file_mime_type) {
                $file_mime_type="application/octet-stream";
            }
            header("Content-Type: " . $file_mime_type);
            header("Content-Disposition: attachment; filename=" . $select_file);
            readfile($tmpfile);
        }
        @unlink($tmpfile);
    }
}

function phpftp_put($phpftp_user,$phpftp_passwd,$phpftp_dir,$userfile,$userfile_name) {
    srand((double)microtime()*1000000);
    $randval = rand();
    $tmpfile=$phpftp_tmpdir . "/" . $userfile_name . "." . $randval;
    if (!@move_uploaded_file($userfile,$tmpfile)) {
        phpftp_top();
?>
<font color="#ff0000"><strong>Upload failed!  Can't create temp file?</strong></font>
<p><p>
<a href="ftp.php">Start over?</a>
<?php
        phpftp_bottom();
    } else {
        if (!$ftp = @phpftp_connect($phpftp_user,$phpftp_passwd)) {
            unlink($tmpfile);
            phpftp_top();
?>
<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>
<a href="ftp.php">Start over?</a>
<?php
            phpftp_bottom();
        } else {
            ftp_chdir($ftp,$phpftp_dir);
            ftp_put($ftp,$userfile_name,$tmpfile,FTP_BINARY);
            ftp_quit($ftp);
            unlink($tmpfile);
            phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir);
        }
    }
}

switch($function) {
    case "dir";
        phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir);
        break;
    case "cd";
        phpftp_cd($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_directory);
        break;
    case "get";
        phpftp_get($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_file);
        break;
    case "put";
        phpftp_put($phpftp_user,$phpftp_passwd,$phpftp_dir,$userfile,$userfile_name);
        break;
    case "mkdir";
        phpftp_mkdir($phpftp_user,$phpftp_passwd,$phpftp_dir,$new_dir);
        break;
    case "";
        phpftp_login();
        break;
}

?>

PHP 相关文章推荐
如何写php程序?
Dec 08 PHP
PHP 多进程 解决难题
Jun 22 PHP
php foreach 使用&amp;(与运算符)引用赋值要注意的问题
Feb 16 PHP
PHP+MYSQL会员系统的登陆即权限判断实现代码
Sep 23 PHP
php生成随机数的三种方法
Sep 10 PHP
ecshop 2.72如何修改后台访问地址
Mar 03 PHP
全面解读PHP的Yii框架中的日志功能
Mar 17 PHP
Adnroid 微信内置浏览器清除缓存
Jul 11 PHP
浅谈PHP的exec()函数无返回值排查方法(必看)
Mar 31 PHP
Yii框架参数化查询中IN查询只能查询一个的解决方法
May 20 PHP
如何解决PHP获取不到SESSION信息之一般情况
Oct 10 PHP
PHP设计模式之组合模式定义与应用示例
Feb 01 PHP
php中文本数据翻页(留言本翻页)
Oct 09 #PHP
自己动手做一个SQL解释器
Oct 09 #PHP
GD输出汉字的函数的分析
Oct 09 #PHP
类的另类用法--数据的封装
Oct 09 #PHP
最小化数据传输――在客户端存储数据
Oct 09 #PHP
网站加速 PHP 缓冲的免费实现方法
Oct 09 #PHP
Windows下PHP的任意文件执行漏洞
Oct 09 #PHP
You might like
php绘制一个扇形的方法
2015/01/24 PHP
Docker 如何布置PHP开发环境
2016/06/21 PHP
PHP微信开发之有道翻译
2016/06/23 PHP
利用php操作memcache缓存的基础方法示例
2017/08/02 PHP
js创建数据共享接口——简化框架之间相互传值
2011/10/23 Javascript
解决Extjs上传图片无法预览的解决方法
2012/03/22 Javascript
基于jQuery的烟花效果(运动相关)点击屏幕出烟花
2012/06/14 Javascript
js文件缓存之版本管理详解
2013/07/05 Javascript
javascript 实现map集合
2015/04/03 Javascript
Jquery跨浏览器文本复制插件Zero Clipboard的使用方法
2016/02/28 Javascript
jquery实现tab选项卡切换效果(悬停、下方横线动画位移)
2017/05/05 jQuery
vue cli2.0单页面title修改方法
2018/06/07 Javascript
浅谈webpack 构建性能优化策略小结
2018/06/13 Javascript
Jquery遍历筛选数组的几种方法和遍历解析json对象,Map()方法详解以及数组中查询某值是否存在
2019/01/18 jQuery
vue在index.html中引入静态文件不生效问题及解决方法
2019/04/29 Javascript
Vue实现表格批量审核功能实例代码
2019/05/28 Javascript
Vue + Element UI图片上传控件使用详解
2019/08/20 Javascript
vue.js实现简单的计算器功能
2020/02/22 Javascript
[04:26]DOTA2上海特锦赛小组赛第二日 TOP10精彩集锦
2016/02/27 DOTA
Python多进程同步Lock、Semaphore、Event实例
2014/11/21 Python
详解Django中的权限和组以及消息
2015/07/23 Python
Python的自动化部署模块Fabric的安装及使用指南
2016/01/19 Python
一个基于flask的web应用诞生 记录用户账户登录状态(6)
2017/04/11 Python
python实现二叉查找树实例代码
2018/02/08 Python
python中(str,list,tuple)基础知识汇总
2018/02/20 Python
django 微信网页授权登陆的实现
2019/07/30 Python
python保留格式汇总各部门excel内容的实现思路
2020/06/01 Python
CSS3教程(2):网页边框半径和网页圆角
2009/04/02 HTML / CSS
白俄罗斯大卖场:21vek.by
2019/07/25 全球购物
如何现实servlet的单线程模式
2014/08/05 面试题
复核员上岗演讲稿
2014/01/05 职场文书
学术会议邀请函范文
2014/01/22 职场文书
环保建议书500字
2014/05/14 职场文书
销售活动策划方案
2014/08/26 职场文书
2014年人民警察入党思想汇报
2014/10/12 职场文书
Python实现机器学习算法的分类
2021/06/03 Python