dongnao3990 2011-10-14 17:21
浏览 43
已采纳

通过FTP将整个目录通过FTP上传到远程主机

I'm using Codeigniter to create a web site, and I tried to create a function to upload the entire directory via FTP to a remote host, but nothing is working

I tried 2 functions I found, but also not working, only few files uploaded, and some files size is 0 bytes

Functions Used :

// 1St Function

public function ftp_copy($src_dir, $dst_dir) {
        $d = dir($src_dir);

        while($file = $d->read()) {

            if ($file != "." && $file != "..") {

                if (is_dir($src_dir."/".$file)) {

                    if (!@ftp_chdir($this->conn_id, $dst_dir."/".$file)) {

                        ftp_mkdir($this->conn_id, $dst_dir."/".$file);
                    }

                    ftp_copy($src_dir."/".$file, $dst_dir."/".$file);
                }
                else {

                    $upload = ftp_put($this->conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY);
                }
            }
        }

        $d->close();
    }


// 2nd Solution from Stackoverflow question
    foreach (glob("/directory/to/upload/*.*") as $filename)
        ftp_put($ftp_stream, basename($filename) , $filename, FTP_BINARY);

Any solution ??

  • 写回答

1条回答 默认 最新

  • duanjue6584 2011-10-14 17:47
    关注

    If you are using codeigniter you can use $this->ftp->mirror() from their FTP Library.

    http://codeigniter.com/user_guide/libraries/ftp.html

    $this->load->library('ftp');
    
    $config['hostname'] = 'ftp.example.com';
    $config['username'] = 'your-username';
    $config['password'] = 'your-password';
    $config['debug']    = TRUE;
    
    $this->ftp->connect($config);
    
    $this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
    
    $this->ftp->close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?