FTP上传 -首先上传文件到的那台电脑得安装ftp

/*↓↓↓↓ add upload ftp file 2014-03-16*/
        /*↓↓↓↓ add 2014-03-16 ftp upload file*/
    var $ftpserver;
      var $ftpusername;
      var $ftpuserpass;
      var $ftpUrl;
    /*↑↑↑↑ add 2014-03-16 ftp upload file*/
    function uploadFtpFile($localfile,$safe_filename){
        $ftp_server = $this->ftpserver;//"hth866.eicp.net";
        $port=21;
        $timeout=30;
        $ftp_user_name =$this->ftpusername;//"tjx";
        $ftp_user_pass =$this->ftpuserpass;//"123";
        $ftp_Url=$this->ftpUrl;
        $conn_id = ftp_connect($ftp_server,$port,$timeout);// or die("Couldn't connect to $ftp_server");
        if($conn_id){
            $login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
            if($login_result){
                $file = $localfile;//'D:/xampp/htdocs/helpdesk/Teap-sheet1测试.xls';
                $remote_file =$ftp_Url.'/'.$safe_filename;
                if (ftp_put($conn_id,$remote_file,$file, FTP_BINARY)) {
                    $result = true;
                } else {
                    $result = false;
                }
                ftp_close($conn_id);
              }else{$result=false;}
          }else{$result=false;}
        return $result;
    }
        /*↓↓↓↓ 添加并注释 2014-03-17*/
//    function downFtpFile($filename){
//        $ftp_server = "122.96.28.77 ";
//        $port=21;
//        $timeout=30;
//        $ftp_user_name ="tjx"; //"zfy";
//        $ftp_user_pass ="123"; //"zhangfangyong,./";
//        $conn_id = ftp_connect($ftp_server,$port,$timeout) or die("Couldn't connect to $ftp_server");
//        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
//        $filename="D:/xampp/htdocs/helpdesk/Teap-sheet12.xls";
//        $web_filename="Teap-sheet1.xls";
//        if(ftp_get($conn_id,$filename,$web_filename,FTP_ASCII)){
//             echo "下载成功\n";
//        }else{
//                 echo "下载失败\n";
//        }
//        ftp_close($conn_id);    
//    }
                /*↑↑↑↑ 添加并注释 2014-03-17*/
        /*↑↑↑↑ add upload ftp file 2014-03-16*/

 

 

 

 

 

 

 

 

    /*add 2014-03-17 only delete runinformation'file*/
    function deleteRuninfoFile($filename) {
            $file=$this->attachmentsDir."/$filename";
            unlink($file);
            //return true;
        }  
    /* add 2014-03-17 only delete runinformation'file*/

    /**
     * @author zhanglingzhi
     * 上传文件
     */
    function uploadfile($upFile) {
        $maxsize = $this->getSetting('attachment_max_size'); //读取/admin/config.php配置中的attachment_max_size
        if ($maxsize) {
            $MAXIMUM_FILESIZE = $maxsize;
        } else {
            $MAXIMUM_FILESIZE = 5 * 1024 * 1024;
        }
        $possibles = $this->getSetting('attachment_types_allowed'); //读取/admin/config.php配置中的attachment_types_allowed
        if (!empty($possibles)) {
            $possibles = explode(",", $possibles);
            $filetype_string = join("|", $possibles);
            $rEFileTypes = "/^\.(" . $filetype_string . "){1}$/i";
        } else {
            $rEFileTypes = "/^\.(zip|rar|jpg|jpeg|gif|png|doc|docx|txt|rtf|pdf|xls|xlsx|ppt|pptx){1}$/i"; //如果没有设置则使用默认值
        }
        $dir_base = $this->attachmentsDir;
        $isFile = is_uploaded_file($upFile['tmp_name']);
        if ($isFile) {
            $arrTemp = explode(".", $upFile['name']);
            $filename_sever = date("his") . rand(100, 999);
            $filename_sever .= "." . $arrTemp[count($arrTemp) - 1];
            $safe_filename = $filename_sever;
            /* 存储上传图片的路径 */
            if ($upFile['size'] <= $MAXIMUM_FILESIZE && preg_match($rEFileTypes, strrchr($safe_filename, '.'))) {
                // 2014-03-13 注释掉 并修改方法
                $isMove = move_uploaded_file($upFile['tmp_name'], $dir_base . "/" . $safe_filename);       
               // echo $upFile['tmp_name']."*******".$dir_base . "/" . $safe_filename; exit;
                if ($isMove) {
                     //$isBackupMove=$this->uploadFtpFile($dir_base . "/" . $safe_filename,$safe_filename);
                    // if($isBackupMove){
                          $result = $safe_filename;
                      //}
//                      else{
//                          $this->deleteRuninfoFile($safe_filename);
//                          $result=FALSE;
//                          //备份服务器文件上传失败
//                      }
                    //上传成功
                } else {
                    $result = FALSE;
                    //上传失败
                }
            } else {
                $result = FALSE;
                //文件过大,或者文件类型不允许上传
            }
        } else {
            $result = FALSE;
            //没有上传文件
        }
        return $result;
    }

posted @ 2014-03-18 22:37  JIN__JIN  阅读(678)  评论(0编辑  收藏  举报