/
FTPUpload.php
181 lines (152 loc) · 4.53 KB
/
FTPUpload.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class FTPUpload
{
public static $ftp_connection;
public static $ftp_server;
public static $ftp_user;
public static $ftp_password;
/**
* Function upload file via FTP
* @param String $dir is directory of uploaded file
* @param String localfile is file upload
* @param String $remotefile is name of uploaded file
*/
public function upload( $dir, $local_file, $remote_file)
{
if (!$this->ftp_is_dir(self::$ftp_connection, $dir)) {
$this->ftp_mkdris(self::$ftp_connection, $dir);
}
try {
ftp_put(self::$ftp_connection, ( $dir . $remote_file ), $local_file, FTP_BINARY);
ftp_close(self::$ftp_connection);
$success = true;
$message = 'Berhasil upload '. $remote_file ;
}catch (Exception $e) {
$success = false;
$message = $e->getMessage();
}
return ['success' => $success, 'message' => $message];
}
/**
* Function preview file from FTP
* @param String $path is url of file
*/
public function preview( $path )
{
$parsing = parse_url( $path );
$explode = explode(".", $parsing['path']);
$ext = end($explode);
if( $this->existFileFTP( $path ) )
{
$pathFtp = 'ftp://'.self::$ftp_user.':'.self::$ftp_password.'@'.self::$ftp_server. "/".( $path ) ;
switch($ext)
{
case 'jpg':
$mime = 'data:image/jpeg';
break;
case 'png':
$mime = 'data:image/png';
break;
case 'pdf':
$mime = 'data:application/pdf';
break;
default:
$mime = 'data:image/jpeg';
}
return htmlentities($mime . ";Base64,".base64_encode(file_get_contents($pathFtp)));
}else{
return 'could not found the file';
}
}
/**
* Function download file from FTP
* @param String $source is source url of file
* @param String $file is name of downloaded file
*/
public function download( $source, $file )
{
$preview = $this->preview( $source );
$explode = explode(",", $preview);
$encodedData = end($explode);
$decodedData = base64_decode($encodedData);
// Actual download.
header('Content-Type: application/force-download');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file."\"");
echo $decodedData;
die();
}
/**
* Function connect to FTP
* @param String $address
* @param String $username
* @param String $password
*/
static function connect($address, $username, $password)
{
self::config($address, $username, $password);
try {
$connection = ftp_connect($address);
ftp_login($connection, $username, $password);
self::$ftp_connection = $connection;
return new static;
} catch (Exception $e) {
return $e->getMessage;
}
}
/**
* Set variable
* @param String $address
* @param String $username
* @param String $password
*/
private static function config($address, $username, $pwd)
{
self::$ftp_server = $address;
self::$ftp_user = $username;
self::$ftp_password = $pwd;
}
/**
* Function ceck exist of file
* @param String $dir
*/
private function existFileFTP( $dir )
{
$res = ftp_size(self::$ftp_connection , $dir );
ftp_close(self::$ftp_connection );
if ($res != -1) {
return true;
}
return false;
}
/**
* Function ceck exist of directory
* @param String $dir
* @param $connection
*/
private function ftp_is_dir($connection, $dir)
{
$pushd = ftp_pwd($connection);
if ($pushd !== false && @ftp_chdir($connection, $dir)) {
ftp_chdir($ftp, $pushd);
return true;
}
return false;
}
/**
* Function make directory
* @param $connection
* @param String $path
*/
private function ftp_mkdris($connection, $path)
{
$str = explode("/", $path);
$dir = "";
foreach ($str as $part) {
$dir .= "/". $part ;
if (!$this->ftp_is_dir($connection, $dir)) {
return ftp_mkdir($connection, $dir);
}
}
}
}