php 一键上传文件,ftp上传和下载到其他服务器

没有前戏,直奔主题羡慕羡慕羡慕



step1:

点导入按钮直接弹出这个框。

html:

<form enctype ="multipart/form-data" method="post" id="fileForm" >
   <input type="file" name="Filedata" id="Filedata"  style='display:none' οnchange="javascript:uploadFile()">
   <a class="vb1-btn-blue vb1-right" οnclick="javascript:openFileBrowse('Filedata');" href="#">导入人群</a>
</form>

js:

//打开文件选择框
function openFileBrowse(fileId){
    var ie=navigator.appName=="Microsoft Internet Explorer" ? true : false;
    if(ie){
        document.getElementById(fileId).click();
    }else{
        var a=document.createEvent("MouseEvents");//FF的处理
        a.initEvent("click", true, true);
        document.getElementById(fileId).dispatchEvent(a);
    }
}

//提交上传
function uploadFile() {
    var formData = new FormData($( "#fileForm" )[0]);
    $.ajax({
        url: apiUri + 'crowdMain/crowdUpload' ,
        type: 'POST',
        jsonp: 'dmpcallback',
        data:  formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (result) {
            if(result.ret==1){
                alert('上传成功');
                window.location.reload();
            }else{
                if(result.ret==0 && result.msg){
                    alert(result.msg);
                }else {
                    alert('上传失败');
                }
            }
        },
        error: function (result) {
            alert('上传失败');
        }
    });
}

php 代码:

随便处理上传吧。


step2:

php上传完以后开始ftp上传了  ftp_put ,, ,

$mftp = new MftpLogic;
$serverFile = C('FTP_INPUT').'/'.$resdata.'|'.$fileInfo['serverrealname'];  //ftp 服务器路径
$file =  $_SERVER['DOCUMENT_ROOT'] . $fileInfo['filePath'] .'/'.$fileInfo['serverrealname'];//本地文件路径
$fresult = $mftp->upload($saveFile,$file);//本地文件路径要注意。

php_ftp 下载文件:

public function crowdDownCookie($id)
{

    $mftp = new MftpLogic;
    //服务器文件路径
    $serverFile = C('FTP_OUTPUT') . '/' . $refileName;
    //本地文件路径
    $refileName = $_SERVER['DOCUMENT_ROOT'] . '/' . C('UPLOAD_PATH') . '/' . $refileName;
    $fresult = $mftp->download($saveFile, $refileName, true);
    if(isset($fresult['code']) && $fresult['code'] == 1){
      $root = $refileName;
      $file_name_new = "cookie.xlsx";  //自定下载文件名称
      if (!file_exists($root)) {   //检查文件是否存在
        throw new \Exception('', 500101);
      } else {
        $file = fopen($root, "r");
        //        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: " . filesize($root));
        header("Content-Disposition: attachment; filename=" . $file_name_new);
        //输出
        echo fread($file, filesize($root));
        fclose($file);exit;
      }
    }
    exit();
  }
}


ftp上传类:

FTP_ASCII  这个选项上传到服务器可能打不开 ,需要ftp_binary  二进制方式,不用 ftp_ascii

class MftpLogic
public function upload($serverFile,$localFile){
  $this->connect();
  $resutl = ftp_put($this->conn_id, $serverFile, $localFile, FTP_ASCII);
  if($resutl == false){
    return $this->mreturn(['code'=>500106]);
  }
  return $this->mreturn(['code'=>1,'msg'=>'上传成功']);
}

public function download($serverFile,$localFile,$issave = false){
  $this->connect();
  $size = ftp_size($this->conn_id,$serverFile);
  if($size < 0){
    return $this->mreturn( ['code'=>500102]);
  } 
  $result = ftp_get($this->conn_id, $localFile,$serverFile, FTP_BINARY);
  if($result == false){
    return $this->mreturn( ['code'=>500103]);
  }
  if($issave == false){
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=" . $localFile);
  } else {
    #todo
  }

  return $this->mreturn( ['code'=>1,'msg'=>'已下载']);
}


function connect($config = array())
{
  $this->initialize($config);
  if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
  {
    return $this->mreturn( ['code'=>500104]);
  }
  if ( ! $this->_login())
  {
    return $this->mreturn( ['code'=>500105]);
  }
  return TRUE;
}


/**
 * 参数处理
 * @param array $config
 */
public function initialize($config = array())
{
  if(empty($config)){
    $config = C('FTP_MAIN');
  }
  foreach ($config as $key => $val)
  {
    $this->$key = $val;
  }
  $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
}

/**
 * login
 * @return bool
 */
function _login()
{
  return @ftp_login($this->conn_id, $this->username, $this->password);
}

public function mreturn ($code){
  ftp_close($this->conn_id);
  return $code;
}


全部完成了。

注意ftp的时候上传和下载的路径,本地需要全路径的。









  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值