Yes - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Yes.php
- <?php
- /**
- * Created by PhpStorm.
- * User: vadim
- * Date: 12/9/18
- * Time: 17:32 PM
- */
- namespace common\components;
- class Ftp
- {
- /**
- * @var int
- */
- private $countStart = 1;
- /**
- * @var int
- */
- protected $countMax = 5;
- /**
- * @var int
- */
- protected $timeout = 90;
- /**
- * @var ftp_connect or null
- */
- private $connect = null;
- /**
- * @param string $host
- * @param int $port
- * @return $this
- * @throws \Exception
- */
- public function init($host, $user, $pass, $port = 21)
- {
- $this->connect = ftp_connect($host, $port, $this->timeout);
- if ($this->connect && isset($this->connect)) {
- $login = $this->login($user, $pass);
- if ($login) {
- return $this;
- }
- }
- throw new \Exception('Ошибка подключения!');
- }
- /**
- * @param string $user
- * @param string $pass
- * @return bool|Ftp
- */
- private function login($user, $pass)
- {
- return ftp_login($this->connect, $user, $pass) ? $this : false;
- }
- /**
- * @return Ftp|null
- */
- public function getCurrentDir()
- {
- return isset($this->connect) && ftp_pwd($this->connect) ? $this : null;
- }
- /**
- * @param string $dir
- * @return bool|Ftp
- * @throws \Exception
- */
- public function setDir($dir)
- {
- if (isset($this->connect)) {
- return ftp_chdir($this->connect, $dir) ? $this : false;
- } else {
- throw new \Exception('Отсутсвует подключение!');
- }
- }
- /**
- * @throws \Exception
- */
- public function close()
- {
- if (isset($this->connect)) {
- ftp_close($this->connect);
- unset($this);
- }
- throw new \Exception('Отсутсвует подключение!');
- }
- /**
- * @param int $value
- * @return $this
- */
- public function setTimeout(int $value)
- {
- if (!isset($this->connect)) {
- $this->timeout = $value;
- } else {
- ftp_set_option($this->connect, FTP_TIMEOUT_SEC, $value);
- }
- return $this;
- }
- /**
- * @param $server_file
- * @param $local_file
- * @param int $mode
- * @return $this
- * @throws \Exception
- */
- public function downloadFile($server_file, $local_file, $mode = FTP_BINARY)
- {
- while ($this->countStart <= $this->countMax) {
- $dwn = $this->ftpGet($server_file, $local_file, $mode);
- $this->countStart++;
- if ($dwn) {
- return $this;
- }
- }
- throw new \Exception('Отсутсвует подключение!');
- }
- /**
- * @param string $server_file
- * @param string $local_file
- * @param int $mode
- * @return bool
- */
- protected function ftpGet($server_file, $local_file, $mode)
- {
- if (isset($this->connect)) {
- return ftp_get($this->connect, $local_file, $server_file, $mode);
- }
- return false;
- }
- /**
- * @param int $value
- * @return $this
- */
- public function setCount(int $value)
- {
- $this->countMax = $value;
- return $this;
- }
- }