php ftp上传下载文件

运行环境:
centos7上使用vsftp搭建ftp 服务器;
ftp 用户名user,密码xxxxxxx;
windows7 上运行PHP脚本,上传文件到ftp服务器和下载文件到本地;
提示:
ftp需要使用多个端口,请注意防火墙状态
vsftp配置pasv模式参考:

https://blog.csdn.net/newborn2012/article/details/15812821/
<?php
/**
 * Created by bing on 2019/9/16
 */

/********************************************
 * MODULE:FTP类
 *******************************************/
class ftp
{
    public $off;             // 返回操作状态(成功/失败)
    public $conn_id;           // FTP连接

    /**
     * 方法: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服务器登陆失败");
      $res=  @ftp_pasv($this->conn_id, 1); // 打开被动模拟
		var_dump($res);
    }

    function getdir()
    {
        $res = ftp_pwd($this->conn_id);
        return $res;
    }

    function get_file_list($directory)
    {
        $res = ftp_nlist($this->conn_id, $directory);
        return $res;
    }

    /**
     * 方法:上传文件
     * @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 "文件上传失败,请检查权限及路径是否正确!";
    }

    /**
     * 方法:下载文件
     * @param $localpath --远程目录
     * @param $remotepath --本地目录
     * @param bool $type
     */
    function down_file($localpath, $remotepath, $type = true)
    {
        $this->off = @ftp_get($this->conn_id, $localpath, $remotepath, 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 = "tmp";
        $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);
    }
}
// class class_ftp end

测试

<?php
/**
 * Created by bing on 2019/9/16
 */
 
include_once("ftp.php");
$ftp = new ftp('172.16.1.58', 21, 'user', 'xxxxxxx');

header("Content-type:text/html;charset=utf-8");

date_default_timezone_set("PRC");
$rootdir = '/resources';
$localdir = 'C:\resources';
download($ftp, $rootdir);
upload($ftp, $localdir);
echo 'ok';
$ftp->close();


//下载
function download($ftp, $rootdir)
{
    $dirs = $ftp->get_file_list($rootdir);
	var_dump($dirs);

    foreach ($dirs as $dir) {
        $childdir = $ftp->get_file_list($dir);
        foreach ($childdir as $child) {
            $localfile = 'C:' . $child;
			$localfile  = str_replace("/","\\",$localfile);
            $remotefile = ltrim($child, '/');

            if (!file_exists($localfile)) {
                $pathinfo = pathinfo($child);
                $dirname = 'C:' . $pathinfo['dirname'];
				$dirname  = str_replace("/","\\",$dirname);
                $result = mkdir($dirname, 0777, true);
                chmod($dirname, 0777);
                echo $localfile . '<<<=====' . $remotefile;
                $ftp->down_file($localfile, $remotefile);
                print "\r\n";
            }
        }
    }
}

//上传
function upload($ftp, $rootdir)
{
    $dirs = getDir($rootdir);
    foreach ($dirs as $child) {
		$remotefile = ltrim($child, 'C:\\');
		$child  = str_replace("/","\\",$child);
		
        $localfile = $child;
        var_dump($child);
		echo '</br>';
        if (ftp_size($ftp->conn_id, $remotefile) <= 0) {
            echo $localfile . '=====>>>' . $remotefile;
            $res = $ftp->up_file($localfile, $remotefile);
			echo '</br>';
        }
    }
}


function searchDir($path, &$files)
{
    if (is_dir($path)) {
        $opendir = opendir($path);
        while ($file = readdir($opendir)) {
            if ($file != '.' && $file != '..') {
                searchDir($path . '/' . $file, $files);
            }
        }
        closedir($opendir);
    }
    if (!is_dir($path)) {
        $files[] = $path;
    }
}

//得到目录名
function getDir($dir)
{
    $files = array();
    searchDir($dir, $files);
    return $files;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值