<?
php
/*
require_once("ftp.php")
//参数为本地文件名,远程文件名,远程服务器,登录名,密码,后可加登录端口及超时时长
$myftp = new ftp("C://a.txt","mail/uaad.txt","192.168.0.17","ftp","ftp");
$myftp->upload();
$myftp = new ftp("xxyy.txt","mail/uaad.txt","192.168.0.17","ftp","ftp");
$myftp->download();
*/
class ftp
{
// tangwf 2008-02-01 add
//ftp类
var $filename ; // 本地要传入的文件
var $remote_file ; // 远程的文件路径及命名
var $ftp_server ; // 远程登录主机
var $ftpport ; // ftp端口
var $ftptime ; // 超时时长
var $MyConnect = false ; // 本次ftp连接
var $username = false ; // 传入的用户名
var $password = false ; // ftp连接密码
//构造函数
function ftp( $filename , $remote_file , $ftp_server , $username , $password , $ftpport = 21 , $ftptime = 90 )
{
$this -> filename = $filename ;
$this -> remote_file = $remote_file ;
$this -> ftp_server = $ftp_server ;
$this -> username = $username ;
$this -> password = $password ;
$this -> ftpport = $ftpport ;
$this -> ftptime = $ftptime ;
}
// 上传方法
function upload()
{
$this -> ftplogin();
if ( ftp_alloc ( $this -> MyConnect , filesize ( $this -> filename) , $result ))
{
if ( ! @ ftp_put ( $this -> MyConnect , $this -> remote_file , $this -> filename , FTP_BINARY))
{
LogTxt( " 上传文件 " . $this -> filename . " 到 " . $this -> ftp_server . $this -> remote_file . " 失败 " );
}
ftp_close ( $this -> MyConnect);
}
else
{
LogTxt( " 上传失败 " . $result );
ftp_close ( $this -> MyConnect);
}
}
// 下载方法
function download()
{
$this -> ftplogin();
if ( ! @ ftp_get ( $this -> MyConnect , $this -> filename , $this -> remote_file , FTP_BINARY))
{
LogTxt( " 下载文件 " . $this -> remote_file . " 到 " . $this -> filename . " 失败 " );
}
ftp_quit ( $this -> MyConnect);
}
// 登录方法
function ftplogin()
{
$this -> ftpconnect();
if ( ! @ ftp_login ( $this -> MyConnect , $this -> username , $this -> password))
{
LogTxt( " 无法登录服务器: " . $this -> ftp_server . " 端口: " . $this -> ftpport . " 请检查用户名密码 " );
ftp_close ( $this -> MyConnect);
}
}
// 连接方法
function ftpconnect()
{
$this -> MyConnect = ftp_connect ( $this -> ftp_server , $this -> ftpport , $this -> ftptime);
if ( ! $this -> MyConnect)
{
LogTxt( " 无法连接服务器: " . $this -> ftp_server . " 端口: " . $this -> ftpport);
}
}
// 写入日志
function LogTxt( $str )
{
$filename = " log " . date ( " Ymd " ) . " .txt " ;
if ( file_exists ( $filename ))
{
if ( ! $handle = fopen ( $filename , ' a ' ))
{
echo " 不能打开文件 $filename " ;
}
}
else
{
if ( ! $handle = fopen ( $filename , ' w+ ' ))
{
echo " 不能创建文件 $filename " ;
}
}
if ( fwrite ( $handle , $str . " " ) === FALSE )
{
echo " 不能写入到log文件 " . $filename ;
}
fclose ( $handle );
}
}
?>
/*
require_once("ftp.php")
//参数为本地文件名,远程文件名,远程服务器,登录名,密码,后可加登录端口及超时时长
$myftp = new ftp("C://a.txt","mail/uaad.txt","192.168.0.17","ftp","ftp");
$myftp->upload();
$myftp = new ftp("xxyy.txt","mail/uaad.txt","192.168.0.17","ftp","ftp");
$myftp->download();
*/
class ftp
{
// tangwf 2008-02-01 add
//ftp类
var $filename ; // 本地要传入的文件
var $remote_file ; // 远程的文件路径及命名
var $ftp_server ; // 远程登录主机
var $ftpport ; // ftp端口
var $ftptime ; // 超时时长
var $MyConnect = false ; // 本次ftp连接
var $username = false ; // 传入的用户名
var $password = false ; // ftp连接密码
//构造函数
function ftp( $filename , $remote_file , $ftp_server , $username , $password , $ftpport = 21 , $ftptime = 90 )
{
$this -> filename = $filename ;
$this -> remote_file = $remote_file ;
$this -> ftp_server = $ftp_server ;
$this -> username = $username ;
$this -> password = $password ;
$this -> ftpport = $ftpport ;
$this -> ftptime = $ftptime ;
}
// 上传方法
function upload()
{
$this -> ftplogin();
if ( ftp_alloc ( $this -> MyConnect , filesize ( $this -> filename) , $result ))
{
if ( ! @ ftp_put ( $this -> MyConnect , $this -> remote_file , $this -> filename , FTP_BINARY))
{
LogTxt( " 上传文件 " . $this -> filename . " 到 " . $this -> ftp_server . $this -> remote_file . " 失败 " );
}
ftp_close ( $this -> MyConnect);
}
else
{
LogTxt( " 上传失败 " . $result );
ftp_close ( $this -> MyConnect);
}
}
// 下载方法
function download()
{
$this -> ftplogin();
if ( ! @ ftp_get ( $this -> MyConnect , $this -> filename , $this -> remote_file , FTP_BINARY))
{
LogTxt( " 下载文件 " . $this -> remote_file . " 到 " . $this -> filename . " 失败 " );
}
ftp_quit ( $this -> MyConnect);
}
// 登录方法
function ftplogin()
{
$this -> ftpconnect();
if ( ! @ ftp_login ( $this -> MyConnect , $this -> username , $this -> password))
{
LogTxt( " 无法登录服务器: " . $this -> ftp_server . " 端口: " . $this -> ftpport . " 请检查用户名密码 " );
ftp_close ( $this -> MyConnect);
}
}
// 连接方法
function ftpconnect()
{
$this -> MyConnect = ftp_connect ( $this -> ftp_server , $this -> ftpport , $this -> ftptime);
if ( ! $this -> MyConnect)
{
LogTxt( " 无法连接服务器: " . $this -> ftp_server . " 端口: " . $this -> ftpport);
}
}
// 写入日志
function LogTxt( $str )
{
$filename = " log " . date ( " Ymd " ) . " .txt " ;
if ( file_exists ( $filename ))
{
if ( ! $handle = fopen ( $filename , ' a ' ))
{
echo " 不能打开文件 $filename " ;
}
}
else
{
if ( ! $handle = fopen ( $filename , ' w+ ' ))
{
echo " 不能创建文件 $filename " ;
}
}
if ( fwrite ( $handle , $str . " " ) === FALSE )
{
echo " 不能写入到log文件 " . $filename ;
}
fclose ( $handle );
}
}
?>