php ftp操作类

来源:互联网 发布:weka 数据预处理class 编辑:程序博客网 时间:2024/05/25 21:33

<这里写代码片?php

namespace modules\Yinzhi\models;
use yii;
/**
* 作用:FTP操作类( 拷贝、移动、删除文件/创建目录,下载 )
* 时间:2017-10-13
* 作者:何为伊人
* QQ:3156976879
*/
class Ftplb {

public $off;  // 返回操作状态(成功/失败) public $conn_id;  // FTP连接public $error;/** * 方法:FTP连接 * @FTP_HOST -- FTP主机 * @FTP_PORT -- 端口 * @FTP_USER -- 用户名 * @FTP_PASS -- 密码 */function __construct($FTP_HOST, $FTP_PORT, $FTP_USER, $FTP_PASS) {    $this->conn_id = @ftp_connect($FTP_HOST, $FTP_PORT) or die("FTP服务器连接失败");    @ftp_login($this->conn_id, $FTP_USER, $FTP_PASS) or die("FTP服务器登陆失败");    @ftp_pasv($this->conn_id, 1); // 打开被动模拟 }/** * 方法:上传文件 * @path-- 本地路径 * @newpath -- 上传路径 * @type-- 若目标目录不存在则新建 */function up_file($path, $newpath, $type = true) {    if ($type)        $this->dir_mkdirs($newpath);    $this->off = @ftp_put($this->conn_id, $newpath, $path, FTP_BINARY);    if (!$this->off)        echo "文件上传失败,请检查权限及路径是否正确!";}/** * 方法:移动文件 * @path-- 原路径 * @newpath -- 新路径 * @type-- 若目标目录不存在则新建 */function move_file($path, $newpath, $type = true) {    if ($type)        $this->dir_mkdirs($newpath);    $this->off = @ftp_rename($this->conn_id, $path, $newpath);    if (!$this->off)        echo "文件移动失败,请检查权限及原路径是否正确!";}/** * 方法:复制文件 * 说明:由于FTP无复制命令,本方法变通操作为:下载后再上传到新的路径 * @path-- 原路径 * @newpath -- 新路径 * @type-- 若目标目录不存在则新建 */function copy_file($path, $newpath, $type = true) {    $downpath = "c:/tmp.dat";    $this->off = @ftp_get($this->conn_id, $downpath, $path, FTP_BINARY); // 下载     if (!$this->off)        echo "文件复制失败,请检查权限及原路径是否正确!";    $this->up_file($downpath, $newpath, $type);}/** * 方法:删除文件 * @path -- 路径 */function del_file($path) {    $this->off = @ftp_delete($this->conn_id, $path);    if (!$this->off)        echo "文件删除失败,请检查权限及路径是否正确!";}/** * 方法:生成目录 * @path -- 路径 */function dir_mkdirs($path) {    $path_arr = explode('/', $path);  // 取目录数组     $file_name = array_pop($path_arr); // 弹出文件名     $path_div = count($path_arr); // 取层数     foreach ($path_arr as $val) {// 创建目录         if (@ftp_chdir($this->conn_id, $val) == FALSE) {            $tmp = @ftp_mkdir($this->conn_id, $val);            if ($tmp == FALSE) {                echo "目录创建失败,请检查权限及路径是否正确!";                exit;            }            @ftp_chdir($this->conn_id, $val);        }    }    for ($i = 1; $i <= $path_div; $i++) {  // 回退到根         @ftp_cdup($this->conn_id);    }}/** * 方法:关闭FTP连接 */function close() {    @ftp_close($this->conn_id);}/** * 验证文件大小、后缀名验证 */function checkfile($uploadfile,$pathurl){    //global $_FILES;    $allow_arr = array('application/pdf','image/gif','image/jpg','image/pjpeg','image/png','image/jpeg','image/bmp','application/vnd.ms-excel','text/plain','application/msword','application/octet-stream','application/x-zip-compressed','text/plain','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/x-rar-compressed');            $extend = pathinfo($uploadfile['name']);    $extend = strtolower($extend["extension"]); //文件后缀名    if(!empty($uploadfile['error']))    {        $this->uploadError($uploadfile['error']);    }else if($uploadfile['size'] > (300*1024*1024)){        $this->error("上传文件超出指定大小!");    }else if(empty($uploadfile['tmp_name']) || $uploadfile['tmp_name'] == 'none'){        $this->error("没有文件被上传..");    }else if(!in_array($uploadfile['type'],$allow_arr)|| $extend =='exe'){        $this->error("上传文件格式有误,该格式文件不允许被上传");    }else{        $fileRand = md5($uploadfile['name'].time().rand(0,99999));        $fileRealName = $fileRand.'.'.$extend;        $upload_file=$pathurl.$fileRealName;    }    $info['error']=$this->error;    $info['filename']=$uploadfile['name'];    $info['filerealname'] = $fileRealName;    $info['filetmpname']=$uploadfile['tmp_name'];    $info['filepath'] = $upload_file;    $info['fileRand'] = $fileRand;    $info['extend'] = $extend;    return $info;}/* * 文件上传错误提示 */function uploadError($errorcode){    switch($errorcode)    {            case '1':                    $this->error = '上传的文件超过了php.ini中upload_max_filesize设置的大小';                    break;            case '2':                    $this->error = '上传文件大小超出了HTML表单的MAX_FILE_SIZE元素所指定的最大值';                    break;            case '3':                    $this->error = '只有部分文件被上传';                    break;            case '4':                    $this->error = '没有文件被上传';                    break;            case '6':                    $this->error = '缺少一个临时文件夹';                    break;            case '7':                    $this->error = '无法写入文件到磁盘';                    break;            case '8':                    $this->error = '文件上传停止';                    break;            case '999':            default:                    $this->error = '';    }}public function download($name,$path){     $file_name=$path; //服务器的真实文件名     $file_realName=urlencode($name); //数据库的文件名urlencode编码过的     $file = fopen($file_name,"r"); // 打开文件     if($file){        header("content-type:text/html; charset=utf-8");                    // 输入文件标签        Header("Content-type: application/octet-stream;charset=gbk");           header("Content-type:application/vnd.ms-excel");                   Header("Accept-Ranges: bytes");        Header("Accept-Length: ".filesize($file_name));        Header("Content-Disposition: attachment; filename=".$file_realName);        // 输出文件内容        // header( "Pragma: public" );        // header( "Expires: 0" );        ini_set('memory_limit','100M');        $contents='';        while (!feof($file)) {            $contents .= fread($file, 4096);        }        echo $contents;        fclose($file);        exit;     }else{        return false;     }}

}// class class_ftp end
/**************************** 测试 *************************
$ftp = new class_ftp(‘192.168.100.143’,21,’user’,’pwd’); // 打开FTP连接

//ftpinfo=ftp->checkfile(FILES[name],path);
//ftp>upfile(aa.txt,a/b/c/cc.txt);////ftp->move_file(‘a/b/c/cc.txt’,’a/cc.txt’); // 移动文件
//ftp>copyfile(a/cc.txt,a/b/dd.txt);////ftp->del_file(‘a/b/dd.txt’); // 删除文件
$ftp->close(); // 关闭FTP连接
********************************************************************/
?>

原创粉丝点击