PHP Remote Attachment Upload

Source: Internet
Author: User
Tags foreach array error code file upload ftp ftp connection integer connect

Today, the SHUIPFCMS program adds remote attachment capabilities. Is the use of FTP implementation, the following posted a phpcms V9 inside an FTP processing class, roughly speaking how to implement the remote attachment.

FTP Class source code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179-18 0 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 The 204 205 Class FTP {             //ftp connection resources      private $link;     //ftp connection time     public $link _time;     //error code     private $err _code = 0;     //Transfer Mode {text mode: FTP_ASCII, Binary mode: ftp_binary}     public $mode = FTP_BINARY;              /**      * Connecting an FTP server      * @param string $host     server address      * @param string $username user name      * @param string $password password      * @param integer $port    server port, the default value is      * @param boolean $PASV    & nbsp;    whether to open the passive mode      * @param boolean $ssl whether to use an SSL connection    &Nbsp; * @param integer $timeout      timeout      */     public function Connect ($host, $username = ', $password = ', $port = ', $PASV = False, $ssl = false, $timeout = {         $start = time ();          if ($SSL) {            if (! $this->link = @ftp _ssl_connect ($host, $port, $timeout)) {                  $this->err_code = 1;                 return false;             }         &NBSP} else {            if (! $this->link = @ Ftp_connect ($host, $port, $timeout)) {                 $this-> Err_code = 1;                 return false;             }          }                   if (@ftp_login ($this->link, $username, $password)) {             if ($PASV)                  FTP_PASV ($this->link, true);              $this->link_time = time ()-$start;             return true;        &NBSP} else {             $this->err_code = 1;             return false;         }         register_ Shutdown_function (Array (& $this, ' close '));    &NBSP}              /**       * Create a folder      * @param string $dirname directory name,     & nbsp;*/    public function mkdir ($dirname) {         if (! $this->link) {             $this->err_code = 2;             return false;        &NBSP}          $dirname = $ This->ck_dirname ($dirname);          $nowdir = '/';         foreach ($dirname as $v) {             if ($v && $this->chdir ($nowdir. $v)) {                 if ($nowdir)                       $this-> ChDir ($nowdir);                  @ftp_mkdir ($ This->link, $v);             }              if ($v)                   $nowdir. = $v. '/';         }       &Nbsp; return true;    &NBSP}              /**       * Upload file      * @param string $remote Remote Storage address     & nbsp;* @param string $local Local address      */    public function put ($ Remote, $local) {        if (! $this->link) {              $this->err_code = 2;             return false;        &NBSP}          $dirname = PathInfo ($remote, pathinfo_dirname);         if (! $this->chdir ($dirname)) {              $this->mkdir ($dirname);       &nbSP;&NBSP}         if (@ftp_put ($this->link, $remote, $local, $this-> mode)) {            return true;         &NBSP} else {            $ This->err_code = 7;             return false;         }                    /**      * Delete folder      * @ param string $dirname   directory address      * @param boolean $enforce Force delete     & nbsp;*/    public function RmDir ($dirname, $enforce = False) {         if (! $this->link) {             $this->err_code = 2;             return false;        &NBSP}          $list = $this- >nlist ($dirname);         if ($list && $enforce) {              $this->chdir ($dirname);             foreach ($list as $v) {                  $this->f_delete ($v);             }         &NBSP} elseif ($list &&! $enforce) {              $this->err_code = 3;             return false;        &NBSP}          @ftp_rmdir ($ This->link, $dirname);         return true;    &NBSP}              /**       * Delete the specified file      * @param string $filename filename       */    public function F_delete ($filename) {         if (! $this->link) {             $this-> Err_code = 2;             return false;        &NBSP}         if (@ftp_ Delete ($this->link, $filename)) {             return true;         &nbsp} else {             $this- >err_code = 4;             return false;         }                    /**      * Returns a list of files for a given directory       * @param string $dirname   directory address      * @return array file list data       */    public function Nlist ($dirname) {         if (! $this->link) {             $this->err_code = 2;             return false;        &NBSP}         if ($list = @ftp _nlist ($thIs->link, $dirname)) {            return $list;        &NBSP} else {              $this->err_code = 5;             return false;         }                    /**      * Changes the current directory on the FTP server       * @param string $dirname Modify the current directory      */    public function on the server ChDir ($dirname) {        if (! $this->link) {              $this->err_code = 2;             return false;        &NBSP}         if (@ftp_chdir ($this->link, $dirname)) {             return true;        &NBSP} else {              $this->err_code = 6;             return false;         }                    /**      * get error messages      */     public function Get_error () {        if (! $this- >err_code)             return false;          $err _msg = Array (              ' 1 ' => ' Server can not connect ',              ' 2 ' => ' Not connect to server ',              ' 3 ' => ' Can not Delete non-empty folder ',              ' 4 ' => ' Can not Delete file ',              ' 5 ' => ' Can not get file List ',              ' 6 ' => ' Can not change the current directory on the server ',              ' 7 ' => ' Can not Upload files '         );         return $err _msg[$this->err_code];    &NBSP}              /**       * Detection Directory name       * @param string $url directory      * @return by/separate return array    & nbsp; */    private function Ck_dirname ($url) {          $url = Str_replace (', '/', $url);          $urls = explode ('/', $url);         return $urls;    &NBSP}              /**       * closes the FTP connection      */    public function close () {         return @ftp_close ($this->link);    &NBSP}         }

First of all, remote attachment upload the approximate process:

User Select File upload submit to server-> server receive file-> server some security detection completed via FTP function upload to the corresponding FTP server.

I said just a general process, not very standard, understand a meaning can! ~

This class uses the method roughly:

First through the $ftps->connect ($host, $username, $password, $post, $PASV, $ssl, $timeout), FTP server connection.

The operation of FTP through specific functions.

$ftps->mkdir () to create a directory, you can create a multi-level directory to "/abc/def/higk" the form of multi-level directory creation.

$ftps->put () upload file

$ftps->rmdir () Delete directory

$ftps->f_delete () Delete files

$ftps->nlist () lists the files for the specified directory

$ftps->chdir () to change the current folder

$ftps->get_error () to get the error message







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.