class_ftp.php


<?php
/**
* 作用:FTP操作类( 拷贝、移动、删除文件/创建目录 )
*/
class class_ftp
{
public $off; // 返回操作状态(成功/失败)
public $conn_id; // FTP连接
const FTP_HOST='*.*.*.*';
const FTP_PORT='21';
const FTP_USER='*******';
const FTP_PASS='*******';
/**
* 方法:FTP连接
* @FTP_HOST -- FTP主机
* @FTP_PORT -- 端口
* @FTP_USER -- 用户名
* @FTP_PASS -- 密码
*/
function __construct()
{
$this->conn_id = @ftp_connect(self::FTP_HOST,self::FTP_PORT) or die("FTP服务器连接失败");
@ftp_login($this->conn_id,self::FTP_USER,self::FTP_PASS) or die("FTP服务器登陆失败");
@ftp_pasv($this->conn_id,1); // 打开被动模拟
}
/**
* 方法:上传文件
* @path -- 本地路径
* @newpath -- 上传路径
* @type -- 若目标目录不存在则新建
*/
function up_file($path,$newpath,$type=true)
{
var_dump($this->conn_id);exit;
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);
}
}// class class_ftp end /************************************** 测试 ***********************************
$ftp = new class_ftp('192.168.100.143',21,'user','pwd'); // 打开FTP连接
//$ftp->up_file('aa.txt','a/b/c/cc.txt'); // 上传文件
//$ftp->move_file('a/b/c/cc.txt','a/cc.txt'); // 移动文件
//$ftp->copy_file('a/cc.txt','a/b/dd.txt'); // 复制文件
//$ftp->del_file('a/b/dd.txt'); // 删除文件
$ftp->close(); // 关闭FTP连接
******************************************************************************/
?>

php的FTP操作类的更多相关文章

  1. PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

  2. C# FTP操作类的代码

    如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...

  3. FTP操作类

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace HGFTP { ...

  4. 很实用的FTP操作类

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...

  5. FTP操作类的使用

    FTP(文件传输协议) FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序 ...

  6. (转)FTP操作类,从FTP下载文件

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  7. FTP操作类(支持异步)

    public delegate void DownloadProgressChangedEventHandle(string information, long currentprogress, lo ...

  8. c#FTP操作类,包含上传,下载,删除,获取FTP文件列表文件夹等Hhelp类

    有些时间没发表文章了,之前用到过,这是我总结出来关于ftp相关操作一些方法,网上也有很多,但是没有那么全面,我的这些仅供参考和借鉴,希望能够帮助到大家,代码和相关引用我都复制粘贴出来了,希望大家喜欢 ...

  9. [转]C# FTP操作类

      转自 http://www.cnblogs.com/Liyuting/p/7084718.html using System; using System.Collections.Generic; ...

随机推荐

  1. 0829NOIP模拟测试赛后总结

    这次发誓不会咕咕咕! 80分rank30完美爆炸. 拿到题目苏轼三连???貌似三篇古诗文我都会背啊hhh.爆零警告 T1没啥思路,打完暴力后想了大约20分钟决定分解个因数,在b次方中每一次方选择一个约 ...

  2. 洛谷 P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold)

    P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold) 就像人类喜欢跳格子游戏一样,FJ的奶牛们发明了一种新的跳格子游戏.虽然这种接近一吨的笨拙的动物玩跳格子游戏几 ...

  3. Android之divider分割线的使用

    1.divider分割线 三种实现方式:(1)添加一个view,(2)通过shape实现,(3)通过设置图片实现 相关属性:设置分割线,分割线位置(none(无),begining(开始),end(结 ...

  4. Catenyms (POJ2337) 字典序最小欧拉路

    // 很久不写图论,连模板也不熟悉了0.0 // 这题是一个技巧性比较高的暴力DFS Catenyms 题目大意 定义catenym为首尾字母相同的单词组成的单词对, 例如: dog.gopher g ...

  5. Python学习day08-python进阶(2)-内置方法

    Python学习day08-python进阶(2)-内置方法 列表数据类型内置方法 作用 描述多个值,比如爱好 定义方法       xxxxxxxxxx 2         1 hobby_list ...

  6. Activiti 部分实用功能

    helloworld中已经写了关于部署流程图,查询个人任务,完成个人任务部分.现在添加几个新的实用功能 1.判断流程是否完成,代码如下 public void isProcessEnd() { Str ...

  7. string字符串 获取指定位置范围的子字符串

    string   str1="12345678";   str1.Substring(0,4);其中0表示要取得字符串的起始位置,4就是要取得字符串的长度  结果是 "1 ...

  8. 2019-6-14-WPF-shows-that-some-windows-in-multithreading-will-be-locked-in-the-PenThreadWorker-constr...

    title author date CreateTime categories WPF shows that some windows in multithreading will be locked ...

  9. Luogu P1429 平面最近点对(加强版)(分治)

    P1429 平面最近点对(加强版) 题意 题目描述 给定平面上\(n\)个点,找出其中的一对点的距离,使得在这\(n\)个点的所有点对中,该距离为所有点对中最小的. 输入输出格式 输入格式: 第一行: ...

  10. leetcode 376Wiggle Subsequence

    用dp解 1)up定义为nums[i-1] < nums[i] down nums[i-1] > nums[i] 两个dp数组, up[i],记录包含nums[i]且nums[i-1] & ...