-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp.class.php
More file actions
133 lines (127 loc) · 4.65 KB
/
ftp.class.php
File metadata and controls
133 lines (127 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// ----------------------------------------------------------------------
// Copyright (C) 2007 by Abdul-Aziz Al-Oraij.
// http://aziz.oraij.com/
// ----------------------------------------------------------------------
// LICENSE
// This program is open source product; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Class Name: FTP Folder Copy/Delete
// Filename: ftp.class.php
// Original Author(s): Abdul-Aziz Al-Oraij <aziz.oraij.com>
// Purpose: Copy to/Delete from FTP folders recursively.
// ----------------------------------------------------------------------
// Bismillah..
class ftp {
var $conn_id;
var $natij=array();
var $orgDir;
function ftp($ftp_server="localhost"){
$this->conn_id = ftp_connect($ftp_server);
}
function connect($user, $pass, $path = "/"){
if(@ftp_login($this->conn_id, $user, $pass) )
return ftp_nlist($this->conn_id, $path);
else
return false;
}
function quit($conn_id = null ){
$conn_id = $conn_id ==null ? $this->conn_id : $conn_id ;
ftp_quit($conn_id);
}
function rmAll($dst_dir, $debug = 0){
if(!$dst_dir) {
return false;
exit;
}
$dst_dir = preg_replace("/\\/\$/", "", $dst_dir); // remove trailing slash
$ar_files = ftp_nlist($this->conn_id, $dst_dir);
if (is_array($ar_files)){ // makes sure there are files
if(count($ar_files)==1) // if its only a file
return ftp_delete($this->conn_id, $ar_files[0]);
else
foreach ($ar_files as $st_file){ // for each file
if ($st_file == "." || $st_file == "..") continue 1;
$fl_file = "$dst_dir/$st_file";
$ftp_size = ftp_size($this->conn_id, $fl_file );
if ($ftp_size == -1){ // check if it is a directory
$this->rmAll($fl_file); // if so, use recursion
}else{
if ($debug )
echo "File: $fl_file | $ftp_size\n";
else
ftp_delete($this->conn_id, $fl_file); // if not, delete the file
}
}
}
if ($debug )
echo "Dir: $dst_dir \n";
elseif(count($ar_files)!=1)
echo @ftp_rmdir($this->conn_id, $dst_dir) ? "$dst_dir deleted!\n":"Can't remove $dst_dir: No such file or directory"; // delete empty directories
}
function log($result) {
if ($result == "print")
foreach ($this->natij as $key => $value){
if ($key == "site" || $key == "put" || $key == "mkdir")
echo "<font color=green>$value ".($key=="put"?"Files uploaded":($key=="mkdir"?"Folders created":"Permissions changed"))." successfully.</font>\n";
else
echo "<font color=red>Failed to: $value </font>\n";
}
else
$result == "site" || $result == "put" || $result == "mkdir" ? $this->natij[$result]++ : $this->natij[] = $result;
}
function get_mod($dir) {
$stat = stat ( $dir );
$mode = substr(decoct ($stat[mode]), -3);
if ($mode=="777" ) $this->log("$dir mode is 777!!");
return $mode;
}
function copy( $local , $remote ) {
if(!$this->orgDir){
$this->orgDir = realpath($local);
if (!is_dir($local)) {
$this->file_copy ( $local , $remote );
return true;
}else{
$local = realpath($local)."/";
if (!@ftp_chdir($this->conn_id, $remote)){
$this->mkdir ( $local , $remote );
}
}
}
if ($open = opendir($local)) {
while (false !== ($file = readdir($open))) {
if ($file != "." && $file != "..") {
$remote_file = $remote.substr(realpath($local.$file), strlen($this->orgDir));
$local_file = $local.$file;
if (!is_dir($local_file)) {
$this->file_copy ( $local_file , $remote_file );
} else {
$this->mkdir ( $local_file , $remote_file );
$this->copy($local . $file . "/" , $remote);
}
}
}
closedir($open);
}
}
function mkdir ( $local , $remote ){
$site = "CHMOD ".$this->get_mod($local)." $remote";
@ftp_mkdir($this->conn_id, $remote)?$this->log("mkdir"):$this->log("mkdir $remote");
ftp_site($this->conn_id, $site) ? $this->log("site") : $this->log($site);
}
function file_copy ( $local , $remote ) {
$site = "CHMOD ".$this->get_mod($local)." $remote";
ftp_put($this->conn_id, $remote, $local, FTP_BINARY)?$this->log("put"):$this->log("put $local in $remote");
ftp_site($this->conn_id, $site)?$this->log("site"):$this->log($site);
}
};
?>