2010-07-09 185 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); 

好的代码! 费尔南多

相关问题