php远程上传实例,PHP ftp类实现远程附件上传例子

本文介绍了如何使用PHP FTP类进行文件上传、目录管理等操作,并结合RSYNC工具实现多服务器数据同步。通过FTP类实现文件从用户上传到目标FTP服务器的转移,同时探讨了RSYNC在Windows环境下的定时同步策略,确保数据的一致性。文章还提供了错误处理和日志记录建议,以确保上传的可靠性。
摘要由CSDN通过智能技术生成

多服务器数据同步并且实时数据处理功能想了很多没找到合适的工具了,今天想了可以使用ftp+rsync工具来实现,下文重点介绍的是php ftp上传类的实现了。

现在很多地方需要用ftp类操作另外的网站服务器,上传图片之类的。现在贴一个php ftp类给大家

class Ftp {

//FTP 连接资源

private $link;

//FTP连接时间

public $link_time;

//错误代码

private $err_code = 0;

//传送模式{文本模式:FTP_ASCII, 二进制模式:FTP_BINARY}

public $mode = FTP_BINARY;

/**

* 连接FTP服务器

* @param string $host    服务器地址

* @param string $username   用户名

* @param string $password   密码

* @param integer $port     服务器端口,默认值为21

* @param boolean $pasv 是否开启被动模式

* @param boolean $ssl      是否使用SSL连接

* @param integer $timeout 超时时间

*/

public function connect($host, $username = '', $password = '', $port = '21', $pasv = false, $ssl = false, $timeout = 30) {

$start = time();

if ($ssl) {

if (!$this->link = @ftp_ssl_connect($host, $port, $timeout)) {

$this->err_code = 1;

return false;

}

} else {

if (!$this->link = @ftp_connect($host, $port, $timeout)) {

$this->err_code = 1;

return false;

}

}

if (@ftp_login($this->link, $username, $password)) {

if ($pasv)

ftp_pasv($this->link, true);

$this->link_time = time() - $start;

return true;

} else {

$this->err_code = 1;

return false;

}

register_shutdown_function(array(&$this, 'close'));

}

/**

* 创建文件夹

* @param string $dirname 目录名,

*/

public function mkdir($dirname) {

if (!$this->link) {

$this->err_code = 2;

return false;

}

$dirname = $this->ck_dirname($dirname);

$nowdir = '/';

foreach ($dirname as $v) {

if ($v && !$this->chdir($nowdir . $v)) {

if ($nowdir)

$this->chdir($nowdir);

@ftp_mkdir($this->link, $v);

}

if ($v)

$nowdir .= $v . '/';

}

return true;

}

/**

* 上传文件

* @param string $remote 远程存放地址

* @param string $local 本地存放地址

*/

public function put($remote, $local) {

if (!$this->link) {

$this->err_code = 2;

return false;

}

$dirname = pathinfo($remote, PATHINFO_DIRNAME);

if (!$this->chdir($dirname)) {

$this->mkdir($dirname);

}

if (@ftp_put($this->link, $remote, $local, $this->mode)) {

return true;

} else {

$this->err_code = 7;

return false;

}

}

/**

* 删除文件夹

* @param string $dirname 目录地址

* @param boolean $enforce 强制删除

*/

public function rmdir($dirname, $enforce = false) {

if (!$this->link) {

$this->err_code = 2;

return false;

}

$list = $this->nlist($dirname);

if ($list && $enforce) {

$this->chdir($dirname);

foreach ($list as $v) {

$this->f_delete($v);

}

} elseif ($list && !$enforce) {

$this->err_code = 3;

return false;

}

@ftp_rmdir($this->link, $dirname);

return true;

}

/**

* 删除指定文件

* @param string $filename 文件名

*/

public function f_delete($filename) {

if (!$this->link) {

$this->err_code = 2;

return false;

}

if (@ftp_delete($this->link, $filename)) {

return true;

} else {

$this->err_code = 4;

return false;

}

}

/**

* 返回给定目录的文件列表

* @param string $dirname 目录地址

* @return array 文件列表数据

*/

public function nlist($dirname) {

if (!$this->link) {

$this->err_code = 2;

return false;

}

if ($list = @ftp_nlist($this->link, $dirname)) {

return $list;

} else {

$this->err_code = 5;

return false;

}

}

/**

* 在 FTP 服务器上改变当前目录

* @param string $dirname 修改服务器上当前目录

*/

public function chdir($dirname) {

if (!$this->link) {

$this->err_code = 2;

return false;

}

if (@ftp_chdir($this->link, $dirname)) {

return true;

} else {

$this->err_code = 6;

return false;

}

}

/**

* 获取错误信息

*/

public function get_error() {

if (!$this->err_code)

return false;

$err_msg = array(

'1' => 'Server can not connect',

'2' => 'Not connect to server',

'3' => 'Can not delete non-empty folder',

'4' => 'Can not delete file',

'5' => 'Can not get file list',

'6' => 'Can not change the current directory on the server',

'7' => 'Can not upload files'

);

return $err_msg[$this->err_code];

}

/**

* 检测目录名

* @param string $url 目录

* @return 由 / 分开的返回数组

*/

private function ck_dirname($url) {

$url = str_replace('', '/', $url);

$urls = explode('/', $url);

return $urls;

}

/**

* 关闭FTP连接

*/

public function close() {

return @ftp_close($this->link);

}

}

先来说说远程附件上传的大致流程:

用户选择文件上传提交到服务器->服务器接收到文件->服务器一些安全检测完成通过FTP功能上传到相应FTP服务器。

我说的只是一个大概过程,不是很标准,明白个意思即可啦!~

这个类大致使用方法:

首先通过 $ftps->connect($host,$username,$password,$post,$pasv,$ssl,$timeout);进行FTP服务器连接。

通过具体的函数进行FTP的操作。

$ftps->mkdir() 创建目录,可以创建多级目录以“/abc/def/higk”的形式进行多级目录的创建。

$ftps->put()上传文件

$ftps->rmdir()删除目录

$ftps->f_delete()删除文件

$ftps->nlist()列出指定目录的文件

$ftps->chdir()变更当前文件夹

$ftps->get_error()获取错误信息

rsync工具同步

这里只介绍原理了rsync同步在windows中只能使用windows计划任务来实现了,我们可以定义为1小时同步一次,这样可以保证同步失败文件再次同步一下,当然在ftp上传类时可以做一个错误日志记录,上传失败之后记录在一个日志文件,然后我们可以手工点击再实现一次上传了,这样台保证万无一失了。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
其中 data\bbscache文件 ftpserver.php 放在程序对应的文件里面 修改文件列表:(PHP文件6个,HTML文件1个) job.php,read.php,show.php,setforum.php,postupload.php,template.php,setforum.htm 1. 数据库升级 进入phpMyAdmin -> SQL 下运行下面升级 ALTER TABLE `pw_forums` ADD `remoteftp` INT( 3 ) UNSIGNED DEFAULT '0' NOT NULL ; ALTER TABLE `pw_attachs` ADD `remoteftp` INT( 3 ) UNSIGNED DEFAULT '0' NOT NULL ; 2. job.php ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆找到require_once('global.php'); ◆下面加入 //远程ftp修改by axing include_once(D_P.'data/bbscache/ftpserver.php'); //远程ftp修改by axing ◆找到 if(!$attach_url && !is_readable("$attachdir/$attachurl")){ Showmsg('job_attach_error'); } ◆替换成 //远程ftp if(!$attach_url && !is_readable("$attachdir/$attachurl") && !$remoteftp){ Showmsg('job_attach_error'); } //远程ftp ◆找到 if($attach_url && !file_exists("$attachdir/$attachurl") && function_exists('file_get_contents')){ $downcontent=file_get_contents($attach_url."/$attachurl"); ◆替换成 if(($attach_url && !file_exists("$attachdir/$attachurl") && function_exists('file_get_contents'))||$remoteftp){ //远程ftp by axing if($remoteftp) { $downcontent=file_get_contents($ftplist[$remoteftp][url]."/$attachurl"); }else{ $downcontent=file_get_contents($attach_url."/$attachurl"); } //远程ftp by axing ◆找到 P_unlink("$attachdir/$attachurl"); ◆替换成 //远程ftp by axing if($remoteftp) { del_ftp_attach($remoteftp,$attachurl); }else { P_unlink("$attachdir/$attachurl"); } //远程ftp by axing 3. read.php ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆找到 require_once(R_P.'require/bbscode.php'); ◆下面加入 //远程ftp修改by axing include_once(D_P.'data/bbscache/ftpserver.php'); //远程ftp修改by axing ◆找到 if ($groupid != 3 && !$foruminfo['allowvisit'] && (!$foruminfo['forumadmin'] || strpos($foruminfo['forumadmin'],','.$windid.',')===false)){ ◆替换成 if ($windid != $manager && $groupid != 3 && !$foruminfo['allowvisit'] && (!$foruminfo['forumadmin'] || strpos($foruminfo['forumadmin'],','.$windid.',')===false)){ ◆找到 $db_signwindcode,$fid,$tid,$pid, ◆后面加入 $remoteftp,$ftplist, ◆找到 $a_url=$attachpath.'/'.$at['attachurl']; ◆下面加入 //远程ftp修改 by axing } elseif($at['remoteftp']){ $a_url=$ftplist[$at['remoteftp']][url].'/'.$at['attachurl'];//远程的图片地址 //远程ftp修改 by axing 4. show.php ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆找到 include_once(D_P.'data/bbscache/forum_cache.php'); ◆下面加入 //远程ftp include_once(D_P.'data/bbscache/ftpserver.php'); //远程ftp ◆找到 a.descrip, ◆后面加入(有两处都要记得改) a.remoteftp, ◆找到 } else{ continue; } ◆上面加入 //远程ftp } elseif($rt['remoteftp']){ $rt['a_url']=$ftplist[$rt['remoteftp']][url].'/'.$rt['attachurl']; //远程ftp ◆找到 } }else{ Showmsg('pic_not_exists'); ◆上面加入 //远程ftp } elseif($rt['remoteftp']){ $rt['a_url']=$ftplist[$rt['remoteftp']][url].'/'.$rt['attachurl']; //远程ftp 5. admin/setforum.php ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆找到 include_once(D_P.'data/bbscache/forumcache.php'); ◆下面加入 //远程ftp修改 by axing include_once("./data/bbscache/ftpserver.php");//引入FTP服务器列表 //远程ftp修改 by axing ◆找到 $viewdownload =str_replace("_{$value}_",'checked',$viewdownload); ◆下面加入 //远程ftp修改 $ftpselected[$remoteftp]='selected'; $ftpselect="<option value=0>本地空间</option>"; foreach($ftplist as $ftpinfo){ $ftpid=$ftpinfo[id]; $ftpselect.="<option value=$ftpinfo[id] $ftpselected[$ftpid]>$ftpinfo[name]</option>"; } //远程ftp修改by axing ◆找到 ifhide='".(int)$ifhide."' ◆后面插入 ,remoteftp='$remoteftp' 6. require/postupload.php ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆找到 !function_exists('readover') && exit('Forbidden'); ◆下面加入 //远程ftp上传修改 by axing include_once(D_P.'data/bbscache/ftpserver.php'); //远程ftp上传修改 by axing ◆找到 $fileuplodeurl= $savedir.'/'.$fileuplodeurl; } ◆替换成 } //远程ftp修改 by axing if($foruminfo['remoteftp']){ $remoteftpid=$foruminfo['remoteftp']; $result = ftpconnect($ftplist[$remoteftpid]); ftp_cdup($result); if($db_attachdir){ if(!@ftp_chdir($result,$savedir)){ ftp_mkdir($result,$savedir); ftp_chdir($result,$savedir); } $attach_fname0=$fileuplodeurl; $fileuplodeurl= $savedir.'/'.$fileuplodeurl; }else{ $attach_fname0=$fileuplodeurl; } //$db->query("INSERT INTO pw_ftperror (attachment, ftpdate, ftpid) VALUES ('$fileuplodeurl', '$timestamp','$foruminfo[remoteftp]')"); //$attid = $db->insert_id(); if(@ftp_put($result, $attach_fname0, $atc_attachment, FTP_BINARY)){ //上传文件 $size=ceil(ftp_size($result,$attach_fname0)/1024); ftp_close($result); // $db->query("delete from pw_ftperror where aid = $attid"); }else{ showmsg('附件远程上传失败!');//远程上传失败 } }else{ //远程ftp修改 by axing $fileuplodeurl= $savedir.'/'.$fileuplodeurl; ◆找到 Showmsg('upload_content_error'); } ◆下面加入 //远程 ftp $size=ceil(filesize("$attachdir/$fileuplodeurl")/1024); } //远程 ftp ◆找到 $size=ceil(filesize("$attachdir/$fileuplodeurl")/1024); $atc_attachment_name=addslashes($atc_attachment_name); $db->update("INSERT INTO pw_attachs SET fid='$fid',uid='$winduid',hits=0,name='$atc_attachment_name',type='$type',size='$size',attachurl='$fileuplodeurl',needrvrc='$needrvrc',uploadtime='$timestamp',descrip='$descrip'"); ◆替换成 $atc_attachment_name=addslashes($atc_attachment_name); //远程ftp $db->update("INSERT INTO pw_attachs SET fid='$fid',uid='$winduid',hits=0,name='$atc_attachment_name',type='$type',size='$size',attachurl='$fileuplodeurl',needrvrc='$needrvrc',uploadtime='$timestamp',descrip='$descrip',remoteftp='$foruminfo[remoteftp]'"); //远程ftp ◆找到 'desc' => str_replace('\\','',$descrip) ◆下面加入 'remoteftp' => $foruminfo[remoteftp] 6.2 如果需要用到txt附件上传时,自动切割并依次上传功能,上面第6步不用修改,直接上传压缩包中的postupload_txt.php 到require/目录即可. 并结合这个插件进行使用.https://blog.csdn.net/viqecel/article/details/79440688 7. require/template.php ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆找到 include_once(D_P."data/bbscache/forumcache.php"); ◆下面加入 //远程ftp include_once(D_P.'data/bbscache/ftpserver.php'); //远程ftp ◆找到 $a_url="$db_bbsurl/$attachpath/$at[attachurl]"; ◆下面加入 //远程ftp }elseif($at['remoteftp']){ $a_url=$ftplist[$at['remoteftp']][url].'/'.$at['attachurl']; //远程ftp 8. template/admin/setforum.htm ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆找到 <tr class=b> <td><input type="checkbox" name="otherforum[logo]" value="1"></td> <td>版块图标</td> <td><input type="text" size="30" name="logo" value="$logo"></td> </tr> ◆下面加入 <tr class=b> <td><input type="checkbox" name="otherforum[remoteftp]" value="1"></td> <td>是否开启远程附件上传</td> <td><select name="remoteftp">$ftpselect</select></td> </tr> (完成...)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值