喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
 15  2/2  <  1  2 
     
标题: FTP操作类( 拷贝、移动、删除文件/创建目录 )  
 
欣然随风
新手上路
Rank: 1



UID 69628
精华 1
积分 25
帖子 23
金钱 15 喜悦币
威望 10
人脉 0
阅读权限 10
注册 2006-3-15
状态 离线
晕倒.发个FTP类,引来的却是命名的讨论.
有啥好讨论的? 每个人都有自己的一套命名规则,自己觉得怎么顺就怎么用.就像你妈生你不会和其它人长得完全一样,但长得漂不漂亮通常有一个大框架的模样,只是细节上有所差异.谋女郎就是这个理.
不要按倒框框死套,那是缺乏创造力和佣人自扰的白痴形为!!




中国中小投资项目网
2006-7-22 03:50 AM#11
查看资料  发短消息  顶部
 
游戏人间
版主
Rank: 7Rank: 7Rank: 7
☞风云山庄大少爷☜


UID 62639
精华 2
积分 14665
帖子 7766
金钱 14515 喜悦币
威望 20
人脉 130
阅读权限 100
注册 2004-12-21
来自 广西人在北京
状态 离线
[推荐阅读] PHP取XML时怎样知得XML的编码?
我也跟着倒.命名嘛,确实是个可爱又可恨的问题.

如果是团队合作的话还是要有统一的规范,还是建议楼主能够规范一些,以便以后跟人合作的时候大家都方便.




 
群号:6025396/6025252/19520091(广西PHP交流/PHP高级编程/算法交流) 寧可在嘗試中失敗,也不在保守中成功! 不為失敗找理由,只為成功找方法! 饿踢Blog
 
2006-7-22 12:02 PM#12
查看资料  访问主页  Blog  发短消息  顶部
 
cyber
新手上路
Rank: 1
超级管理员


UID 15380
精华 0
积分 7
帖子 1163
金钱 7 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2002-7-21
状态 离线
[推荐阅读] 自己研究了几天,没搞明白,只好来这里问了。。


<?
/**
 * FTP Class 
 * version 0.50
 * Copyright Raditha Dissanayake 2003
 * Licence Jabber Open Source Licence.
 *
 * vist [url]http://www.raditha.com/[/url] for updates.
 */

 /*
  * This version of the ftp class works only with unix/linux ftp servers using
  * PASV mode. Active mode support and compatibility with IIS servers would be
  * added at a later date.
  *
  * This class does have any dependencies and you do not need to compile php 
  * with FTP support.
  *
  * I will be delighted to hear about how you have made use of this class but i 
  * have no interest in hearing about your configuration woes :-)
  *
  */
  
DEFINE (FPERM,0);
DEFINE (FINODE,1);
DEFINE (FUID,2);
DEFINE (FGID,3);
DEFINE (FSIZE,4);
DEFINE (FMONTH,5);
DEFINE (FDAY,7);
DEFINE (FTIME,7);
DEFINE (FNAME,8);

class 
FTP {

    var 
$host;
    
    var 
$sock;
    var 
$data_sock;
    
    var 
$errno;
    var 
$errstr;
    var 
$message;

    var 
$user="anonymous";
    var 
$pass="[email]nobody@nobody.com[/email]";
        
    var 
$enable_log=0;
    

    
/**
     * constructor
     */
    
function FTP($host,$port=21)
    {
        
$this->log("host = $host, $port=$port");
        
$this->host $host;
        
$this->port $port;
    }
    
    
/**
     * connects to the server and returns the socket which will
     * be null if the connection failed. In which case you should
     * look at the $errno and $errstr variables.
     */
    
function connect()
    {
        
        
$this->sock = @fsockopen($this->host,$this->port,&$this->errno,&$this->errstr,30);
        @
set_socket_blocking($this->sock,false);
        return 
$this->sock;
    }


    
/**
     * will read data from the socket. Since we are using non blocking 
     * mode, if we attempt to read the data even a millisecond before 
     * it becomes available, the method just returns null.
     *
     * So what's the cure? we will loop for a while and try to read it
     * in multiple times. Note that if we used blocking mode there may
     * be instances when this function NEVER returns.
     */
    
function sock_read($i=0)
    {
        
$s fgets($this->sock,4096);

        
$this->log($s);
        if(
$s=='')
        {
            if(
$i==30)
            {
                return 
"";
            }
            else
            {
                
sleep(1);
                return 
$this->sock_read($i+1);
            }
        }
        return 
$s;
    }

    
/**
     * move up the directory tree
     */
    
function cdup()
    {
        
$this->sock_write("CDUP");
        return 
is_ok();
    }

    
    
/**
     * change working directory
     */
    
function cwd($path)
    {
        
$this->sock_write("CWD $path");
        return 
$this->check_reply("250");
    }

    
/**
     * create new directory
     */
    
function mkd($path)
    {
        
$this->sock_write("MKD $path");
        return 
$this->check_reply("257");
    }
    
    
/*
     * remove a directory
     */
    
function rmd($path)
    {
        
$this->sock_write("RMD $path");
        return 
$this->check_reply("250");
    }
    
    
/**
     * print working directory. Returns a string that contains
     * the path to the current directory.
     */
    
function pwd()
    {
        
$this->sock_write("PWD");
        if(
$this->check_reply("257"))
        {
            
$parts split(" ",$this->message);
            return 
preg_replace('/\"/',"",$parts[1]);
        }
        return 
"";
    }
        
    
/**
     * this is a hack. We are just checking if the server returns a 
     * 5xx number which indentifies an error. We assume that 
     * everything else is ok. Which may not always be true. That's 
     * some functionality which should be improved in the future.
     */
     
    
function is_ok()
    {
        
$this->message $this->sock_read();

        if(
$this->message == "" || preg_match('/^5/',$this->message) )
        {
            return 
0;
        }
        else
        {
            return 
1;
        }
    }

    
/**
     * Utility method so that we can take care of logging and
     * adding the carriage return etc.
     */
    
function sock_write($s)
    {
        @
fputs($this->sock,"$sn");
        
$this->log($s);
        
    }

    
/**
     * retrieves a listing of the given directory or the current
     * working directory if no path has been specified. Returns
     * a string which contains all the directory entries separated
     * by new lines.
     */
    
function dir_list($path="")
    {
        
$s="";
        if(
$this->pasv())
        {
            if(
$path == '')
            {
                
$this->sock_write("LIST");
            }
            else
            {
                
$this->sock_write("LIST $path");
            }
            if(
$this->is_ok())
            {
                while(
true)
                {
                    
$line fgets($this->data_sock);
                    
$s .= $line;
                    if(
$line =='')
                        break;
                }
            }
        }
        return 
$s;
    }
    
    
/**
     * this function will return a handle that can be used to 
     * retrieve a file on the FTP host. 
     */
    
function retr($path)
    {
        if(
$this->pasv())
        {
            
$this->sock_write("RETR $path");
            if(
$this->is_ok())
            {
                return 
$this->data_sock;
            }
        }
        return 
"";
    }
    
    
/**
     * stores the file given in $localpath on the remote server as
     * $remotePath
     */
    
function stor($localPath,$remotePath)
    {
        
log("<h3>uploading $localPath</h3>");
        
        
        if(
$this->pasv())
        {
            
$this->sock_write("STOR $remotePath");
            if(
$this->is_ok())
            {
                
$fp fopen($localPath,"rb");
                
                
                if(
$fp)
                {
                    
                    while(!
feof($fp))
                    {
                        
$s fread($fp,4096);
                        
fwrite($this->data_sock,$s);
                    }
                    return 
1;
                }
                return 
0;
            }
        }
    }
    
/**
     * establishes a passive mode data connection
     */
    
function pasv()
    {
        
$this->sock_write("PASV");
        if(
$this->is_ok())
        {
            
$offset strpos($this->message,"(");
            
            
$s substr($this->message,++$offset,strlen($this->messsage)-2);
            
$parts split(",",trim($s));
            
$data_host "$parts[0].$parts[1].$parts[2].$parts[3]";
            
$data_port = ((int)$parts[4] << 8) + (int) $parts[5];
        
            
$this->data_sock fsockopen($data_host,$data_port,&$errno,&$errstr,30);
            return 
$this->data_sock;
        }
        return 
"";
    }
    
    
    
    
/**
     * log the user in with the given username and password, default
     * to annoymous with a dud email address if no username password
     * have been given.
     */
    
function login($user="",$pass="")
    {
        
/* no overloading in PHP */
        
if($user==''$user $this->user;
        if(
$pass==''$pass $this->pass;
        
        
$this->sock_write("USER $user");
        if(
$this->is_ok())
        {
            
$this->sock_write("PASS $pass");

            if(
$this->check_reply("230"))
            {
                
                return 
1;
            }
        }

        
ob_flush();
        return 
0;
    }
    
    
/**
     * match the response from the server with the given status code
     */
    
function check_reply($code)
    {
        if(
$code=='230')
        {
            while(
1)
            {
                if(
$this->is_ok())
                {
                    if( 
preg_match('/^230 /',$this->message))
                    {
                        return 
1;
                    }
                    
                }                        
                else
                {
                    return 
0;
                }

            }
        }
        else
        {
            if(
$this->is_ok())
            {
                
$pat '/^'$code .'/';
                if( 
preg_match($pat,$this->message))
                {
                    return 
1;
                }
                
            }    
            return 
0;
        }
    }
    
    
/**
     * Rename From this file ...
     */
    
function rnfr($filename)
    {
        
sock_write("RNFR $filename");
        if(
is_ok())
        {
            return 
1;
        }
        else
        {
            return 
0;
        }
    }
    
    
/**
     * .. rename to this file
     */
    
function rnto($filename)
    {
        
sock_write("RNTO $filename");
        if(
is_ok())
        {
            return 
1;
        }
        else
        {
            return 
0;
        }
    }
    
/**
     * change this method to suite your log system.
     * be sure to setting the $enable_log to 1 
     */
    
function log($str)
    {
        if(
$this->enable_log)
        {
            echo 
"$str<br>n";
        }
    }
    
    
/**
     * login with this user name..
     */
    
    
function set_user($user)
    {
        
$this->user=$user;
    }
    
/**
     * .. and this password.
     */
     
    
function set_pass($pass)
    {
        
$this->pass =$pass;
    }
    
}

/**
 * this is not a member of the FTP class because it does not
 * work directly with the FTP server in any way. It will instead
 * process the output from the FTP server.
 *
 * Notice that the Table does not have the table start and end
 * tag. this is to allow you to create a header outside of this
 * function.
 *
 * Overall i think template schemes of this nature suck but to
 * use my beloved XSLT would be overkill.
 */
function show_list($data)
{

    
$list split("n",$data);

    
$pattern "/[dwrx-]{10}/";
    
    
$list array_slice($list,1,count($list)-1);
    foreach(
$list as $file)
    {
        
        
$file preg_split("/ /",$file,20,PREG_SPLIT_NO_EMPTY);
        
        
/*
         * directory download has to be implemented in a different manner
         * so we will just ignore directories for now.
         */
        
if(preg_match('/^d/',trim($file[FPERM])))
        {
            
$downlink "dir.php?dir="urlencode(trim("$path".$file[FNAME]));    
        }
        else
        {
            
$downlink "getfile.php?filename="urlencode("$path".$file[FNAME]);
        }
        
        
printf('<tr><td class="cell1">%s</td><td class="cell1">%s</td>
                    <td class="cell1">%s</td><td class="cell1">%s</td>
                    <td class="cell1">%s %s %s</td>
                    <td  class="cell1"><a href="%s">%s</a></td>
                </tr>'
,
                
$file[FPERM],$file[FUID],$file[FGID], $file[FSIZE],
                
$file[FMONTH],$file[FDAY],$file[FTIME],
                
$downlink$file[FNAME]);

    }
}


?>
转的,供参考




中国第一个PHP专业搜索引擎
2006-7-22 03:45 PM#13
查看资料  访问主页  发短消息  QQ . .   顶部
 
phpqingang
注册会员
Rank: 2
冷月


UID 23517
精华 0
积分 180
帖子 1514
金钱 180 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2003-9-28
来自 水星
状态 离线
[推荐阅读] 查询结果用表格形式显示出来这种效果是如何做的呢?
这个类根本不通用!!!
2006-7-24 01:15 PM#14
查看资料  Blog  发短消息  顶部
 
风暴一族
新手上路
Rank: 1



UID 74407
精华 0
积分 2
帖子 2
金钱 2 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-6-1
状态 离线
[推荐阅读] 这两款服务器谁优谁劣?
很好!~要支持



我爱搜罗娱乐网http://www.51solo.net/
2006-7-28 11:04 AM#15
查看资料  访问主页  发短消息  顶部
 15  2/2  <  1  2 
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.080699 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP