2010-07-09 183 views

回答

1

我對CodeIgniter的FTP類一無所知,但這又如何呢?

http://www.php.net/manual/en/function.ftp-rawlist.php

我假設FTP類的list_files()方法不提供此信息。那是對的嗎?

+0

我認爲他想堅持CI的FTP功能('ftp_rawlist()'可能需要在使用PHP的內置函數打開的FTP連接時使用)。但是,它看起來像CI實際上不提供功能。 – 2010-07-09 09:26:08

+0

@Pekka:CI FTP類是標準PHP FTP函數的簡單包裝類,所以很容易擴展它並添加一個執行ftp_rawlist的額外包裝函數。 CI的list_files只是調用ftp_nlist btw。 – wimvds 2010-07-09 11:04:26

+0

@wim啊,有道理。 – 2010-07-09 12:09:54

-1

使用file helper

get_file_info('path/to/file', $file_information) 

給定一個文件名和路徑,返回名稱,路徑,大小,修改日期。第二個參數允許你顯式聲明你想要返回的信息;選項有:名稱,服務器路徑,大小,日期,可讀,可寫,可執行文件,文件。如果無法找到文件,則返回FALSE。

+0

我不明白這是如何通過FTP連接工作? – 2010-07-09 09:25:22

+0

@pekka它不,我認爲這是一個誤讀。 – DRL 2010-07-09 10:36:49

0

其相對簡單的擴展CI FTP類:

class MY_FTP extends CI_FTP { 

    function MY_FTP() 
    { 
     parent::CI_FTP(); 
    } 

    function get_file_size() 
    { 

    } 
} 

基本上只是讓get_ftp_size()的包裝爲:

return ftp_size($conn, $file); 

http://php.net/manual/en/function.ftp-size.php

我希望這有助於(如果您通過你安裝的ftp.php文件被卡住了;你應該很快找到你的方式)

編輯

由於wimvds正確建議的ftp_rawlist()可能是更理想/更容易的選擇,我可能甚至走那麼遠,建議改變list_files()使用ftp_rawlist()。

+0

爲[ftp_rawlist](http://www.php.net/manual/en/function.ftp-rawlist.php)添加包裝可能會更容易,因此您可以將文件名和大小都設置爲一個FTP命令...您將不得不解析結果(因爲它只是每個文件返回一個字符串)。 – wimvds 2010-07-09 11:01:33

2

functionshould工作剛剛有了一個快速瀏覽一下CodeIgniters FTP類。至於它與PHP4向後compat的,如果你要,你可以probabaly做到這一點(破解工作)寫的。

<?php 
$files = $this->ftp->list_files('/folder/'); 

foreach ($files as $file) 
{ 
    echo 'File:'. $file .' Size: '. ftp_size($this->ftp->conn_id, $file) .' <br />'; 
} 
$this->ftp->close(); 

我不建議這一點 - 這是probabably值得推廣的主要CI ftp類

class FTP extends CI_FTP 
{ 

    function FTP() 
    { 
    // call parent constructor 
    parent::CI_FTP(); 
    } 

    // Single file size 
    function file_size($file) 
    { 
    return ftp_size($this->conn_id, $file); 
    } 
} 

把上面到應用程序/庫,並將其保存爲ftp.php。如果您運行CI的更新版本,它會加載您的擴展。

0

由於CI仍是PHP 4兼容的,你也許可以做到快速和骯髒如下:

$this->load->library('ftp'); 

$config['hostname'] = 'ftp.example.com'; 
$config['username'] = 'your-username'; 
$config['password'] = 'your-password'; 
$config['debug'] = TRUE; 

$this->ftp->connect($config); 
$files = ftp_rawlist($this->ftp->conn_id, $path); 

在$文件,你應該得到每個文件一行,包含文件權限,所有者/組,文件大小和文件名。你將不得不解析這個分開顯示它們。免責聲明:只需從CI手冊複製/粘貼連接信息,添加基於CI源代碼YMMV:p的最後一行即可。

0

經過努力,這段代碼很好用!我想和社區分享(通過MundialSYS)

function dirFTPSize($ftpStream, $dir) { 
    $size = 0; 
    $files = ftp_nlist($ftpStream, $dir); 

    foreach ($files as $remoteFile) { 
     if(preg_match('/.*\/\.\.$/', $remoteFile) || preg_match('/.*\/\.$/', $remoteFile)){ 
      continue; 
     } 
     $sizeTemp = ftp_size($ftpStream, $remoteFile); 
     if ($sizeTemp > 0) { 
      $size += $sizeTemp; 
     }elseif($sizeTemp == -1){//directorio 
      $size += dirFTPSize($ftpStream, $remoteFile); 
     } 
    } 

    return $size; 
} 

$hostname = '127.0.0.1'; // or 'ftp.domain.com' 
$username = 'username'; 
$password = 'password'; 
$startdir = '/public_html'; // absolute path 
$files = array(); 
$ftpStream = ftp_connect($hostname); 
$login = ftp_login($ftpStream, $username, $password); 
if (!$ftpStream) { 
    echo 'Wrong server!'; 
    exit; 
} else if (!$login) { 
    echo 'Wrong username/password!'; 
    exit; 
} else { 
    $size = dirFTPSize($ftpStream, $startdir); 
} 

echo number_format(($size/1024/1024), 2, '.', '') . ' MB'; 

ftp_close($ftpStream); 

好的代碼! 費爾南多

相關問題