50 if (!function_exists(ftp_connect))
52 $this->_errorMsg =
'ftp extension missing';
56 $this->_errorMsg =
'';
58 $oldErrorLevel = error_reporting (E_ERROR | E_PARSE);
60 $conn_id = ftp_connect($params[
'server'], $params[
'port'], 10);
64 $login_result = ftp_login($conn_id, $params[
'login'], $params[
'password']);
68 $this->_ftpConnId = $conn_id;
70 $this->_ftpRootDir = ftp_pwd($this->_ftpConnId);
77 $this->_errorMsg =
'login failed';
81 $this->_errorMsg =
'connection to server failed';
83 error_reporting ($oldErrorLevel);
94 function getFiles($directory=
'.', $pattern=
'/./', $prependDirectoryName=
false, $recursive=
false)
96 if ($this->_ftpConnId == null)
98 $this->_errorMsg =
'connect to server first';
103 $isfile = ftp_size($this->_ftpConnId, urldecode($directory));
107 $fileList = ftp_nlist($this->_ftpConnId, $directory);
108 foreach ($fileList as $file)
110 $isfile = ftp_size($this->_ftpConnId, urldecode($file));
111 if($recursive && $isfile ==
"-1")
113 $files = $this->
getFiles($file , $pattern, $prependDirectoryName, $recursive);
114 $result = array_merge($result, $files);
116 else if($isfile !=
"-1" && preg_match($pattern, $file))
118 if (!$prependDirectoryName)
119 $file = substr($file, strrpos($file,
'/')+1);
120 array_push($result, $file);
125 $this->_errorMsg =
"The directory '".$directory.
"' does not exist.";
135 if ($this->_ftpConnId == null)
137 $this->_errorMsg =
'connect to server first';
141 ftp_chdir($this->_ftpConnId, $this->_ftpRootDir);
143 $file = urldecode($file);
144 $fileInfo[
'size'] = ftp_size($this->_ftpConnId, $file);
145 $fileInfo[
'mtime'] = ftp_mdtm($this->_ftpConnId, $file);
158 function copy($file, $transferMode, $toDir=
'./', $createDir=
true, $destName=null)
160 if ($this->_ftpConnId == null)
162 $this->_errorMsg =
'connect to server first';
166 $this->_errorMsg =
'';
168 $oldErrorLevel = error_reporting (E_ERROR | E_PARSE);
169 if ($this->_ftpConnId != null && file_exists($file))
172 if ($destName == null)
173 $destName = basename($file);
176 if ($toDir[0] !=
'/')
177 ftp_chdir($this->_ftpConnId, $this->_ftpRootDir);
178 if (!ftp_chdir($this->_ftpConnId, $toDir))
182 ftp_mkdir($this->_ftpConnId, $toDir);
183 ftp_chdir($this->_ftpConnId, $toDir);
187 $this->_errorMsg =
'change dir failed: '.$toDir;
193 $upload = ftp_put($this->_ftpConnId, $destName, $file, $transferMode);
196 $this->_errorMsg =
'file upload failed';
201 error_reporting ($oldErrorLevel);
214 if ($this->_ftpConnId == null)
216 $this->_errorMsg =
'connect to server first';
220 $localInfo = stat($localFile);
225 if ($remoteInfo[
'size'] == -1 || $remoteInfo[
'size'] != $localInfo[
'size'] ||
226 $remoteInfo[
'mtime'] == -1 || $remoteInfo[
'mtime'] < $localInfo[
'mtime'])
228 $pathInfo = pathinfo($remoteFile);
229 $transfered = $this->
copy($localFile, FTP_BINARY, $pathInfo[
'dirname'],
true, $pathInfo[
'basename']);
240 function delete($file)
242 if ($this->_ftpConnId == null)
244 $this->_errorMsg =
'connect to server first';
248 return ftp_delete($this->_ftpConnId, $file);
255 $this->_errorMsg =
'';
257 $oldErrorLevel = error_reporting (E_ERROR | E_PARSE);
259 if ($this->_ftpConnId != null)
260 ftp_quit($this->_ftpConnId);
261 $this->_ftpConnId = null;
263 error_reporting ($oldErrorLevel);
FTPUtil provides support for ftp functionality.
synchronize($localFile, $remoteFile)
getFiles($directory='.', $pattern='/./', $prependDirectoryName=false, $recursive=false)
copy($file, $transferMode, $toDir='./', $createDir=true, $destName=null)