2011-10-14 42 views
0

我正在使用Codeigniter創建一個網站,我試圖創建一個函數來通過FTP上傳整個目錄到遠程主機,但沒有任何工作通過PHP將整個目錄通過PHP上傳到遠程主機通過FTP

我試過2個功能,我發現,但上傳也沒有工作,只有少數的文件,以及一些文件的大小爲0字節

函數中使用:

// 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 ([email protected]_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); 

任何解決方案?

+1

什麼是你正在使用的功能? – Ben

+0

我添加了函數,thx – Hamza

回答

4

如果您使用的是codeigniter,您可以使用FTP庫中的$ this-> ftp-> mirror()。

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(); 
+0

我正在使用鏡像函數,但我傳遞的是沒有結尾斜槓的源目錄,現在我添加了尾部斜槓並且它正在工作。 謝謝 – Hamza

+0

框架的實用性再次發揮作用。 – Xeoncross