php实现通过ftp上传文件
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
error_reporting(7);
if ($_SERVER['SERVER_ADDR'])exit;//禁止在web服务器下运行
$_GET['exclude'] = array('number.txt','uploads','Zend','docs','cache','You','managesdk'); //排除上传目录,定义为全局变量
$fileobj = new FilerFile();
$path = "/data/longtu/"; //项目目录的根目录
$files = $fileobj->Zip($path); //过滤出最新的修改文件
$path = str_replace("/data/longtu/","",$path);
$config = array(
'hostname' => 'xxx.xxx.xx.xxx', //ftp服务器 地址
'username' => 'xxx', //ftp用户
'password' => '?xxxxxxxxxxx', //ftp密码
'port' => 21 //端口
);
$ftp = new Ftp();
$ftp->connect($config); //链接服务器
//$a=$ftp->filelist();
$LOCAL_ROOT = realpath(dirname(__DIR__)."/../../");
chdir($LOCAL_ROOT);
foreach ($files as $k=>$v){
$f = $path.$v;
$tmp = $ftp->upload($f, $f);
if($tmp){
echo "upload $f -> success \n";
}
}
//$ftp->download('ftp_upload.log','ftp_download.log');
//
//$ftp->upload('ftp_err.log','ftp_upload.log');
//$ftp->download('ftp_upload.log','ftp_download.log');
/*
*
*
* $dir = "/test";
if(@ftp_chdir($conn, $dir))
判断是否为文件夹
* Enter description here ...
* @author Administrator
*
*/
class FilerFile
{
var $time_path;
private $fctimes = array();
function Zip($dir)
{
$this->time_path = rtrim($dir,"/")."/.~~~time.php";
//@unlink($this->time_path);
$filelist = $this -> GetFileList($dir);
file_put_contents($this->time_path,"<?php \n return ".var_export($this->fctimes,true).";");
return $filelist;
}
function appendFilectime($file)
{
$time_file_path = $this->time_path;
$ftime = @include($time_file_path);
$ftime = $ftime ? $ftime : array();
$time = filectime($file);
if(!file_exists($time_file_path))file_put_contents($time_file_path,"<?php \n");
}
function getFileByFilectime($file)
{
static $time_data ;
$time_file_path = $this->time_path;
if (!$time_data){
$time_data= @include_once($time_file_path);
}
$time_data = $time_data ? $time_data : array();
//var_dump($file,$time_data[$file] == filectime($file));
//echo $file."\t".$time_data[$file]."\n";
if ($time_data[$file] == filemtime($file)){
return false;
}else{
return $file;
}
}
function GetFileList($dir,$path="")
{
static $tmpp = "";
if ($path=="" && !$tmpp){
$tmpp = $dir;
}
$d = dir($dir);
$files = array();
if ($d)
{
$pathP=str_replace($tmpp,"",$dir);
$pathP=str_replace(array("\\\\","/"),DIRECTORY_SEPARATOR,$pathP);
$pathP=str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$pathP);
while($f = $d->read())
{
if ($f == '.' || $f=='..' || $f{0}=='.' || $f=='Zend' || in_array($f, $_GET['exclude']))continue;
$newdir = rtrim($dir,"/")."/".$f;
if (is_dir($newdir)){
$files = array_merge($files,$this->GetFileList($newdir,$newdir));
}else{
$abspath_file = (rtrim($dir,"/") ? rtrim($dir,"/")."/" : "").$f;
$this->fctimes[$abspath_file] = filemtime($abspath_file);
if (!$this->getFileByFilectime($abspath_file))continue;
$file = (rtrim($pathP,"/") ? rtrim($pathP,"/")."/" : "").$f;
$files[] = $file;
}
}
}
return $files;
}
}
/**
* 仿写CodeIgniter的FTP类
* FTP基本操作:
* 1) 登陆; connect
* 2) 当前目录文件列表; filelist
* 3) 目录改变; chgdir
* 4) 重命名/移动; rename
* 5) 创建文件夹; mkdir
* 6) 删除; delete_dir/delete_file
* 7) 上传; upload
* 8) 下载 download
*
* @author quanshuidingdang
*/
class Ftp {
private $hostname = '';
private $username = '';
private $password = '';
private $port = 21;
private $passive = TRUE;
private $debug = TRUE;
private $conn_id = FALSE;
/**
* 构造函数
*
* @param array 配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
*/
public function __construct($config = array()) {
if(count($config) > 0) {
$this->_init($config);
}
}
/**
* FTP连接
*
* @access public
* @param array 配置数组
* @return boolean
*/
public function connect($config = array()) {
if(count($config) > 0) {
$this->_init($config);
}
if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_connect");
}
return FALSE;
}
if( ! $this->_login()) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_login");
}
return FALSE;
}
if($this->passive === TRUE) {
ftp_pasv($this->conn_id, TRUE);
}
return TRUE;
}
/**
* 文件夹是否存在
* @param unknown_type $path
*/
public function is_dir_exists($path)
{
return $this->chgdir($path);
}
/**
* 目录改变
*
* @access public
* @param string 目录标识(ftp)
* @param boolean
* @return boolean
*/
public function chgdir($path = '', $supress_debug = FALSE) {
if($path == '' OR ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_chdir($this->conn_id, $path);
if($result === FALSE) {
if($this->debug === TRUE AND $supress_debug == FALSE) {
$this->_error("ftp_unable_to_chgdir:dir[".$path."]");
}
return FALSE;
}
return TRUE;
}
/**
* 目录生成
*
* @access public
* @param string 目录标识(ftp)
* @param int 文件权限列表
* @return boolean
*/
public function mkdir($path = '', $permissions = NULL) {
if($path == '' OR ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_mkdir($this->conn_id, $path);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_mkdir:dir[".$path."]");
}
return FALSE;
}
if( ! is_null($permissions)) {
$this->chmod($path,(int)$permissions);
}
return TRUE;
}
/**
* 上传
*
* @access public
* @param string 本地目录标识