2011-07-05 27 views
8

ftp_ssl_connect tutabilir TLS üzerinden örtülü FTP? Varsayılan olarak açık kullanır.ftp_ssl_connect üstü kapalı ftp ile tls üzerinden

Yalnızca 990 numaralı bağlantı noktasında tls üzerinden Implicit ftp kabul eden bir sunucuya yüklemeye çalışıyorum; Buna henüz giren var mı? Nasıl tamir ettin?

+1

IIRC, örtülü olarak desteklenmez. Sanırım kendime birkaç kez girdim. – Orbling

+0

Bunun yerine kıvrılma işlevlerini denemek isteyebilirsiniz. Bunun örtük olarak işe yarayıp yaramayacağından emin değilim, ancak genel olarak size seçenekler biraz daha fazla kontrol sağlıyor. –

+0

düzenleme ve yorumlar için teşekkürler – Rasiel

cevap

13

ftp_ssl_connect size örtülü gerekirse

, kullanmak yalnızca müstehcen kıvrılma hızlı bir çözüm bu sayfayı üzerine google olur ve isteyen herkes için

$fp = fopen($path, 'r'); 
$ftp_server = 'ftps://'.$server.'/'.$filename; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $ftp_server); 
curl_setopt($ch, CURLOPT_USERPWD,$user.':'.$pass); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); 
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS); 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 

$output = curl_exec($ch); 
$error_no = curl_errno($ch); 
//var_dump(curl_error($ch)); 
curl_close($ch); 
3

:

Ben konısidır cevabı genişletilmiş ve bu yöntemi kullanarak temel yükleme/indirme için basit bir sınıf yaptı. Umut ediyorum bu yardım eder!

<?php 

class ImplicitFtp { 

    private $server; 
    private $username; 
    private $password; 

    public function __construct($server, $username, $password) { 
     $this->server = $server; 
     $this->username = $username; 
     $this->password = $password; 
    } 

    public function download($remote, $local = null) { 
     if ($local === null) { 
      $local = tempnam('/tmp', 'implicit_ftp'); 
     } 

     if ($fp = fopen($local, 'w')) { 
      $ftp_server = 'ftps://' . $this->server . '/' . $remote; 
      $ch = curl_init(); 

      curl_setopt($ch, CURLOPT_URL, $ftp_server); 
      curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
      curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); 
      curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS); 
      curl_setopt($ch, CURLOPT_UPLOAD, 0); 
      curl_setopt($ch, CURLOPT_FILE, $fp); 

      curl_exec($ch); 

      if (curl_error($ch)) { 
       curl_close($ch); 
       return false; 
      } else { 
       curl_close($ch); 
       return $local; 
      } 
     } 
     return false; 
    } 

    public function upload($local, $remote) { 
     if ($fp = fopen($local, 'r')) { 
      $ftp_server = 'ftps://' . $this->server . '/' . $remote; 
      $ch = curl_init(); 

      curl_setopt($ch, CURLOPT_URL, $ftp_server); 
      curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
      curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); 
      curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS); 
      curl_setopt($ch, CURLOPT_UPLOAD, 1); 
      curl_setopt($ch, CURLOPT_INFILE, $fp); 

      curl_exec($ch); 
      $err = curl_error($ch); 
      curl_close($ch); 

      return !$err; 
     } 
     return false; 
    } 

} 
4

Frustrum ve Steven Jeffries'in yanıtlarına dayanarak daha da genişlettim. Bu, curl bağlantısını yeniden kullanır ve son değiştirilene göre dosyaları sıralamak dahil olmak üzere bazı dizin listeleme işlevlerine sahiptir.

Bu, PHP 7 içindir, daha düşük için < => operatör satırını yeniden yazmanız gerekecektir.

<?php 
/** 
* Implicit FTP 
* @author Nico 
* Based on 
* http://stackoverflow.com/questions/6589730/ftp-ssl-connect-with-implicit-ftp-over-tls 
* http://stackoverflow.com/questions/845220/get-the-last-modified-date-of-a-remote-file 
*/ 
class ImplicitFtp { 

    private $server; 
    private $username; 
    private $password; 
    private $curlhandle; 

    public function __construct($server, $username, $password) { 
     $this->server = $server; 
     $this->username = $username; 
     $this->password = $password; 
     $this->curlhandle = curl_init(); 
    } 

    public function __destruct() { 
     if (!empty($this->curlhandle)) 
      @curl_close($this->curlhandle); 
    } 

    /** 
    * @param string $remote remote path 
    * @return resource a cURL handle on success, false on errors. 
    */ 
    private function common($remote) { 
     curl_reset($this->curlhandle); 
     curl_setopt($this->curlhandle, CURLOPT_URL, 'ftps://' . $this->server . '/' . $remote); 
     curl_setopt($this->curlhandle, CURLOPT_USERPWD, $this->username . ':' . $this->password); 
     curl_setopt($this->curlhandle, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($this->curlhandle, CURLOPT_SSL_VERIFYHOST, FALSE); 
     curl_setopt($this->curlhandle, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); 
     curl_setopt($this->curlhandle, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS); 
     return $this->curlhandle; 
    } 

    public function download($remote, $local = null) { 
     if ($local === null) { 
      $local = tempnam('/tmp', 'implicit_ftp'); 
     } 

     if ($fp = fopen($local, 'w')) { 
      $this->curlhandle = self::common($remote); 
      curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 0); 
      curl_setopt($this->curlhandle, CURLOPT_FILE, $fp); 

      curl_exec($this->curlhandle); 

      if (curl_error($this->curlhandle)) { 
       return false; 
      } else { 
       return $local; 
      } 
     } 
     return false; 
    } 

    public function upload($local, $remote) { 
     if ($fp = fopen($local, 'r')) { 
      $this->curlhandle = self::common($remote); 
      curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 1); 
      curl_setopt($this->curlhandle, CURLOPT_INFILE, $fp); 

      curl_exec($this->curlhandle); 
      $err = curl_error($this->curlhandle); 

      return !$err; 
     } 
     return false; 
    } 

    /** 
    * Get file/folder names 
    * @param string $remote 
    * @return string[] 
    */ 
    public function listnames($remote) { 
     if (substr($remote, -1) != '/') 
      $remote .= '/'; 
     $this->curlhandle = self::common($remote); 
     curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 0); 
     curl_setopt($this->curlhandle, CURLOPT_FTPLISTONLY, 1); 
     curl_setopt($this->curlhandle, CURLOPT_RETURNTRANSFER, 1); 

     $result = curl_exec($this->curlhandle); 

     if (curl_error($this->curlhandle)) { 
      return false; 
     } else { 
      $files = explode("\r\n", trim($result)); 
      return $files; 
      return $local; 
     } 
    } 

    /** 
    * Get file/folder names ordered by modified date 
    * @param string $remote 
    * @return string[] 
    */ 
    public function listbydate($remote) { 
     $files = $this->listnames($remote); 
     if (empty($files)) 
      return null; 
     $filedata = array(); 
     foreach ($files as $file) { 

      $this->curlhandle = self::common($remote . '/' . $file); 
      curl_setopt($this->curlhandle, CURLOPT_NOBODY, 1); 
      curl_setopt($this->curlhandle, CURLOPT_FILETIME, 1); 
      curl_setopt($this->curlhandle, CURLOPT_RETURNTRANSFER, 1); 
      $result = curl_exec($this->curlhandle); 

      if ($result) { 
       $timestamp = curl_getinfo($this->curlhandle, CURLINFO_FILETIME); 
       $fileobj = array(); 
       $fileobj['name'] = $file; 
       $fileobj['lastmodified'] = ($timestamp != -1) ? date("Y-m-d H:i:s", $timestamp) : null; 
       $filedata[] = $fileobj; 
      } 
     } 

     usort($filedata, function ($item1, $item2) { 
      return date($item2['lastmodified']) <=> date($item1['lastmodified']); 
     }); 

     return $filedata; 
    } 



    /** 
    * Get file/folder raw data 
    * @param string $remote 
    * @return string[] 
    */ 
    public function rawlist($remote) { 
     if (substr($remote, -1) != '/') 
      $remote .= '/'; 
     $this->curlhandle = self::common($remote); 
     curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 0); 
     curl_setopt($this->curlhandle, CURLOPT_RETURNTRANSFER, 1); 

     $result = curl_exec($this->curlhandle); 

     if (curl_error($this->curlhandle)) { 
      return false; 
     } else { 
      $files = explode("\n", trim($result)); 
      return $files; 
      return $local; 
     } 
    } 

    /** 
    * Get file/folder parsed data into an array 
    * @param string $remote 
    * @return array[] 
    */ 
    public function list($remote) { 
     $this->curlhandleildren = $this->rawlist($remote); 
     if (!empty($this->curlhandleildren)) { 
      $items = array(); 
      foreach ($this->curlhandleildren as $this->curlhandleild) { 
       $chunks = preg_split("/\s+/", $this->curlhandleild); 
       list($item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time']) = $chunks; 
       array_splice($chunks, 0, 8); 
       $item['name'] = trim(implode(" ", $chunks)); 
       $item['type'] = $chunks[0]{0} === 'd' ? 'directory' : 'file'; 
       $items[] = $item; 
      } 
      return $items; 
     } 
     return false; 
    } 

} 
?> 
+0

Özel işlev ortak ($ ​​uzak) '' '' özel statik işlev ortak ($ ​​uzak) '' PHP’nin durağan yöntem operatörünü kullanarak çağırdığınızda '' kendi kendine ortak ($ ​​uzak); '? – tonix

+0

@tonix self :: Statik olmayan aramalara izin verilir - bkz. Https://stackoverflow.com/questions/19218182/php-calling-self-on-a-non-static-method –