Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NFtp
  • NHtml
  • NHttpContext
  • NHttpRequest
  • NHttpResponse
  • NHttpUploadedFile
  • NSession
  • NSessionNamespace
  • NUri
  • NUriScript
  • NUser

Interfaces

  • IHttpRequest
  • IHttpResponse
  • IUser

Exceptions

  • NFtpException
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Web
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Access to a FTP server.
 17:  *
 18:  * <code>
 19:  * $ftp = new Ftp;
 20:  * $ftp->connect('ftp.example.com');
 21:  * $ftp->login('anonymous', 'example@example.com');
 22:  * $ftp->get('file.txt', 'README', NFtp::ASCII);
 23:  * </code>
 24:  *
 25:  * @author     David Grudl
 26:  * @package Nette\Web
 27:  */
 28: class NFtp extends NObject
 29: {
 30:     /**#@+ FTP constant alias */
 31:     const ASCII = FTP_ASCII;
 32:     const TEXT = FTP_TEXT;
 33:     const BINARY = FTP_BINARY;
 34:     const IMAGE = FTP_IMAGE;
 35:     const TIMEOUT_SEC = FTP_TIMEOUT_SEC;
 36:     const AUTOSEEK = FTP_AUTOSEEK;
 37:     const AUTORESUME = FTP_AUTORESUME;
 38:     const FAILED = FTP_FAILED;
 39:     const FINISHED = FTP_FINISHED;
 40:     const MOREDATA = FTP_MOREDATA;
 41:     /**#@-*/
 42: 
 43:     /** @var resource */
 44:     private $resource;
 45: 
 46:     /** @var array */
 47:     private $state;
 48: 
 49: 
 50: 
 51:     /**
 52:      */
 53:     public function __construct()
 54:     {
 55:         if (!extension_loaded('ftp')) {
 56:             throw new Exception("PHP extension FTP is not loaded.");
 57:         }
 58:     }
 59: 
 60: 
 61: 
 62:     /**
 63:      * Magic method (do not call directly).
 64:      * @param  string  method name
 65:      * @param  array   arguments
 66:      * @return mixed
 67:      * @throws MemberAccessException
 68:      * @throws NFtpException
 69:      */
 70:     public function __call($name, $args)
 71:     {
 72:         $name = strtolower($name);
 73:         $silent = strncmp($name, 'try', 3) === 0;
 74:         $func = $silent ? substr($name, 3) : $name;
 75:         static $aliases = array(
 76:             'sslconnect' => 'ssl_connect',
 77:             'getoption' => 'get_option',
 78:             'setoption' => 'set_option',
 79:             'nbcontinue' => 'nb_continue',
 80:             'nbfget' => 'nb_fget',
 81:             'nbfput' => 'nb_fput',
 82:             'nbget' => 'nb_get',
 83:             'nbput' => 'nb_put',
 84:         );
 85:         $func = 'ftp_' . (isset($aliases[$func]) ? $aliases[$func] : $func);
 86: 
 87:         if (!function_exists($func)) {
 88:             return parent::__call($name, $args);
 89:         }
 90: 
 91: 
 92:         NTools::tryError();
 93: 
 94:         if ($func === 'ftp_connect' || $func === 'ftp_ssl_connect') {
 95:             $this->state = array($name => $args);
 96:             $this->resource = call_user_func_array($func, $args);
 97:             $res = NULL;
 98: 
 99:         } elseif (!is_resource($this->resource)) {
100:             NTools::catchError($msg);
101:             throw new NFtpException("Not connected to FTP server. Call connect() or ssl_connect() first.");
102: 
103:         } else {
104:             if ($func === 'ftp_login' || $func === 'ftp_pasv') {
105:                 $this->state[$name] = $args;
106:             }
107: 
108:             array_unshift($args, $this->resource);
109:             $res = call_user_func_array($func, $args);
110: 
111:             if ($func === 'ftp_chdir' || $func === 'ftp_cdup') {
112:                 $this->state['chdir'] = array(ftp_pwd($this->resource));
113:             }
114:         }
115: 
116:         if (NTools::catchError($msg) && !$silent) {
117:             throw new NFtpException($msg);
118:         }
119: 
120:         return $res;
121:     }
122: 
123: 
124: 
125:     /**
126:      * Reconnects to FTP server.
127:      * @return void
128:      */
129:     public function reconnect()
130:     {
131:         @ftp_close($this->resource); // intentionally @
132:         foreach ($this->state as $name => $args) {
133:             call_user_func_array(array($this, $name), $args);
134:         }
135:     }
136: 
137: 
138: 
139:     /**
140:      * Checks if file or directory exists.
141:      * @param  string
142:      * @return bool
143:      */
144:     public function fileExists($file)
145:     {
146:         return is_array($this->nlist($file));
147:     }
148: 
149: 
150: 
151:     /**
152:      * Checks if directory exists.
153:      * @param  string
154:      * @return bool
155:      */
156:     public function isDir($dir)
157:     {
158:         $current = $this->pwd();
159:         try {
160:             $this->chdir($dir);
161:         } catch (NFtpException $e) {
162:         }
163:         $this->chdir($current);
164:         return empty($e);
165:     }
166: 
167: 
168: 
169:     /**
170:      * Recursive creates directories.
171:      * @param  string
172:      * @return void
173:      */
174:     public function mkDirRecursive($dir)
175:     {
176:         $parts = explode('/', $dir);
177:         $path = '';
178:         while (!empty($parts)) {
179:             $path .= array_shift($parts);
180:             try {
181:                 if ($path !== '') $this->mkdir($path);
182:             } catch (NFtpException $e) {
183:                 if (!$this->isDir($path)) {
184:                     throw new NFtpException("Cannot create directory '$path'.");
185:                 }
186:             }
187:             $path .= '/';
188:         }
189:     }
190: 
191: }
192: 
193: 
194: 
195: /**
196:  * FTP server exception.
197:  *
198:  * @package Nette\Web
199:  */
200: class NFtpException extends Exception
201: {
202: }
203: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0