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.



Your result can be seen below.

Result of php executing





Full code of Yes.php

  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: vadim
  5.  * Date: 12/9/18
  6.  * Time: 17:32 PM
  7.  */
  8.  
  9. namespace common\components;
  10.  
  11. class Ftp
  12. {
  13.     /**
  14.      * @var int
  15.      */
  16.     private $countStart = 1;
  17.  
  18.     /**
  19.      * @var int
  20.      */
  21.     protected $countMax = 5;
  22.  
  23.     /**
  24.      * @var int
  25.      */
  26.     protected $timeout = 90;
  27.  
  28.     /**
  29.      * @var ftp_connect or null
  30.      */
  31.     private $connect = null;
  32.  
  33.     /**
  34.      * @param string $host
  35.      * @param int $port
  36.      * @return $this
  37.      * @throws \Exception
  38.      */
  39.     public function init($host, $user, $pass, $port = 21)
  40.     {
  41.         $this->connect = ftp_connect($host, $port, $this->timeout);
  42.         if ($this->connect && isset($this->connect)) {
  43.             $login = $this->login($user, $pass);
  44.             if ($login) {
  45.                 return $this;
  46.             }
  47.         }
  48.         throw new \Exception('Ошибка подключения!');
  49.     }
  50.  
  51.     /**
  52.      * @param string $user
  53.      * @param string $pass
  54.      * @return bool|Ftp
  55.      */
  56.     private function login($user, $pass)
  57.     {
  58.         return ftp_login($this->connect, $user, $pass) ? $this : false;
  59.     }
  60.  
  61.     /**
  62.      * @return Ftp|null
  63.      */
  64.     public function getCurrentDir()
  65.     {
  66.         return isset($this->connect) && ftp_pwd($this->connect) ? $this : null;
  67.     }
  68.  
  69.     /**
  70.      * @param string $dir
  71.      * @return bool|Ftp
  72.      * @throws \Exception
  73.      */
  74.     public function setDir($dir)
  75.     {
  76.         if (isset($this->connect)) {
  77.             return ftp_chdir($this->connect, $dir) ? $this : false;
  78.         } else {
  79.             throw new \Exception('Отсутсвует подключение!');
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * @throws \Exception
  85.      */
  86.     public function close()
  87.     {
  88.         if (isset($this->connect)) {
  89.             ftp_close($this->connect);
  90.             unset($this);
  91.         }
  92.         throw new \Exception('Отсутсвует подключение!');
  93.     }
  94.  
  95.     /**
  96.      * @param int $value
  97.      * @return $this
  98.      */
  99.     public function setTimeout(int $value)
  100.     {
  101.         if (!isset($this->connect)) {
  102.             $this->timeout = $value;
  103.         } else {
  104.             ftp_set_option($this->connect, FTP_TIMEOUT_SEC, $value);
  105.         }
  106.         return $this;
  107.     }
  108.  
  109.     /**
  110.      * @param $server_file
  111.      * @param $local_file
  112.      * @param int $mode
  113.      * @return $this
  114.      * @throws \Exception
  115.      */
  116.     public function downloadFile($server_file, $local_file, $mode = FTP_BINARY)
  117.     {
  118.         while ($this->countStart <= $this->countMax) {
  119.             $dwn = $this->ftpGet($server_file, $local_file, $mode);
  120.             $this->countStart++;
  121.  
  122.             if ($dwn) {
  123.                 return $this;
  124.             }
  125.         }
  126.         throw new \Exception('Отсутсвует подключение!');
  127.     }
  128.  
  129.     /**
  130.      * @param string $server_file
  131.      * @param string $local_file
  132.      * @param int $mode
  133.      * @return bool
  134.      */
  135.     protected function ftpGet($server_file, $local_file, $mode)
  136.     {
  137.         if (isset($this->connect)) {
  138.             return ftp_get($this->connect, $local_file, $server_file, $mode);
  139.         }
  140.         return false;
  141.     }
  142.  
  143.     /**
  144.      * @param int $value
  145.      * @return $this
  146.      */
  147.     public function setCount(int $value)
  148.     {
  149.         $this->countMax = $value;
  150.         return $this;
  151.     }
  152. }
File Description
  • Yes
  • PHP Code
  • 26 Jan-2023
  • 3.22 Kb
You can Share it: