30 throw new \Exception(__CLASS__ .
" - no connection established with server");
43 return @ftp_mkdir($this->_conn,
$name);
56 $dir = explode(
"/",
$path);
59 for (
$i = 0;
$i < count($dir);
$i++) {
61 if (!@ftp_chdir($this->_conn,
$path)) {
62 @ftp_chdir($this->_conn,
"/");
63 if (!@ftp_mkdir($this->_conn,
$path)) {
85 $res = @ftp_login($this->_conn, $login, $password);
87 throw new \Exception(
"Invalid login credentials");
101 $data = parse_url($string);
102 if (
false ===
$data) {
103 throw new \Exception(
"Connection string invalid: '{$string}'");
105 if (
$data[
'scheme'] !=
'ftp') {
106 throw new \Exception(
"Support for scheme unsupported: '{$data['scheme']}'");
110 foreach (array_intersect(array_keys(
$data), [
'user',
'pass']) as $key) {
127 public function connect($string, $timeout = 900)
132 $this->_conn = ftp_connect(
$params[
'host'], $port, $timeout);
135 throw new \Exception(
"Cannot connect to host: {$params['host']}");
144 throw new \Exception(
"Cannot chdir after login to: {$params['path']}");
161 return @ftp_fput($this->_conn, $remoteFile,
$handle,
$mode, $startPos);
173 public function put($remoteFile, $localFile,
$mode = FTP_BINARY, $startPos = 0)
176 return ftp_put($this->_conn, $remoteFile, $localFile,
$mode, $startPos);
186 $d = $this->
raw(
"pwd");
187 $data = explode(
" ", $d[0], 3);
188 if (empty(
$data[1])) {
191 if (intval(
$data[0]) != 257) {
194 $out = trim(
$data[1],
'"');
196 $out = rtrim($out,
"/");
210 return @ftp_raw($this->_conn, $cmd);
226 public function upload($remote, $local, $dirMode = 0777, $ftpMode = FTP_BINARY)
230 if (!file_exists($local)) {
231 throw new \Exception(
"Local file doesn't exist: {$local}");
234 throw new \Exception(
"Local file is not readable: {$local}");
237 throw new \Exception(
"Directory given instead of file: {$local}");
240 $globalPathMode = substr($remote, 0, 1) ==
"/";
241 $dirname = dirname($remote);
243 if (
false === $cwd) {
244 throw new \Exception(
"Server returns something awful on PWD command");
247 if (!$globalPathMode) {
248 $dirname = $cwd .
"/" . $dirname;
249 $remote = $cwd .
"/" . $remote;
257 return $this->
put($remote, $local, $ftpMode);
268 public function download($remote, $local, $ftpMode = FTP_BINARY)
271 return $this->
get($local, $remote, $ftpMode);
283 return @ftp_pasv($this->_conn, (
bool)$pasv);
294 @ftp_close($this->_conn);
308 return @ftp_chmod($this->_conn,
$mode, $remoteFile);
320 return @ftp_chdir($this->_conn, $dir);
331 return @ftp_cdup($this->_conn);
344 public function get($localFile, $remoteFile, $fileMode = FTP_BINARY, $resumeOffset = 0)
348 return @ftp_get($this->_conn, $localFile, $remoteFile, $fileMode, $resumeOffset);
361 return @ftp_nlist($this->_conn, $dir);
371 public function rawlist($dir =
"/", $recursive =
false)
375 return @ftp_rawlist($this->_conn, $dir, $recursive);
386 $symbol = [
'B',
'KB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB'];
387 $exp = floor(log($bytes) / log(1024));
388 return sprintf(
'%.2f ' . $symbol[$exp], $bytes / pow(1024, floor($exp)));
399 $trans = [
'-' =>
'0',
'r' =>
'4',
'w' =>
'2',
'x' =>
'1'];
400 $chmod = substr(strtr($chmod, $trans), 1);
401 $array = str_split($chmod, 3);
402 return array_sum(str_split($array[0])) . array_sum(str_split($array[1])) . array_sum(str_split($array[2]));
415 $globalPathMode = substr(
$path, 0, 1) ==
"/";
417 $file = basename(
$path);
421 if ($file ==
$row[
'name']) {
422 if ($excludeIfIsDir &&
$row[
'dir']) {
439 public function ls($dir =
"/", $recursive =
false)
442 $rawfiles = (array)$this->
rawlist($dir, $recursive);
444 $arraypointer = & $structure;
445 foreach ($rawfiles as $rawfile) {
446 if ($rawfile[0] ==
'/') {
447 $paths = array_slice(explode(
'/', str_replace(
':',
'', $rawfile)), 1);
448 $arraypointer = & $structure;
450 foreach ($arraypointer as
$i => $file) {
451 if ($file[
'name'] ==
$path) {
452 $arraypointer = & $arraypointer[
$i][
'children'];
457 }
elseif (!empty($rawfile)) {
458 $info = preg_split(
"/[\s]+/", $rawfile, 9);
461 'dir' =>
$info[0][0] ==
'd',
462 'size' => (int)
$info[4],
463 'chmod' => self::chmodnum(
$info[0]),
480 $str = str_replace(
"\\",
"/", $str);
481 $str = preg_replace(
"/^.\//",
"", $str);
491 public function delete($file)
495 return @ftp_delete($this->_conn, $file);
mkdirRecursive($path, $mode=0777)
static byteconvert($bytes)
rawlist($dir="/", $recursive=false)
validateConnectionString($string)
elseif(isset( $params[ 'redirect_parent']))
upload($remote, $local, $dirMode=0777, $ftpMode=FTP_BINARY)
fput($remoteFile, $handle, $mode=FTP_BINARY, $startPos=0)
download($remote, $local, $ftpMode=FTP_BINARY)
chmod($mode, $remoteFile)
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
fileExists($path, $excludeIfIsDir=true)
ls($dir="/", $recursive=false)
connect($string, $timeout=900)
put($remoteFile, $localFile, $mode=FTP_BINARY, $startPos=0)
foreach( $_productCollection as $_product)() ?>" class $info
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
if(!isset($_GET['name'])) $name