php 写个上传下载类,保留备用

本文介绍了一个简单的PHP FTP类实现,该类能够完成文件的上传和下载功能,并记录操作日志。通过设置FTP服务器地址、用户名密码等参数,可以轻松地将本地文件上传至远程服务器或将远程文件下载至本地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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);
    }
}
?> 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值