没有前戏,直奔主题。
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的时候上传和下载的路径,本地需要全路径的。