非常教程

Codeigniter 3参考手册

FTP

FTP Class

CodeIgniter的FTP类允许文件传输到远程服务器。远程文件也可以被移动,重命名和删除。FTP类还包含一个“镜像”功能,允许通过FTP远程重建整个本地目录。

注意

不支持SFTP和SSL FTP协议,只有标准FTP。

  • 使用FTP类
    • 初始化类
    • 用法示例
  • 类参考

使用FTP类

初始化类

像CodeIgniter中的大多数其他类一样,FTP类在您的控制器中使用$ this-> load-> library函数进行初始化:

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

一旦加载,FTP对象将可用:$ this-> ftp

用法示例

在这个例子中,向FTP服务器打开一个连接,并且以ASCII模式读取和上传本地文件。文件权限设置为755。

$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->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);

$this->ftp->close();

在这个例子中,从服务器中检索一个文件列表。

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

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

$this->ftp->connect($config);

$list = $this->ftp->list_files('/public_html/');

print_r($list);

$this->ftp->close();

在这个例子中,本地目录在服务器上被镜像。

$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();

类参考

class CI_FTPconnect([$config = array()])

参数:

$ config(array) - 连接值

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ configarray) - 连接值
Returns:  TRUE on success, FALSE on failure
Return type:  bool
连接并登录到FTP服务器。连接首选项是通过将数组传递给函数来设置的,或者可以将它们存储在配置文件中。

以下是一个显示如何手动设置首选项的示例:

$this->load->library('ftp'); $config'hostname' = 'ftp.example.com'; $config'username' = 'your-username'; $config'password' = 'your-password'; $config'port' = 21; $config'passive' = FALSE; $config'debug' = TRUE; $this->ftp->connect($config);

在配置文件中设置FTP首选项

如果您愿意,您可以将您的FTP首选项存储在配置文件中。只需创建一个名为ftp.php的新文件,在该文件中添加$ config数组。然后将文件保存在application / config / ftp.php文件中,它将自动使用。

可用的连接选项

选项名称默认值说明**主机名**不适用FTP主机名(通常类似于:ftp.example.com)**用户名**不适用FTP用户名**密码**不适用FTP密码**端口* * 21 FTP服务器端口号** debug ** FALSE TRUE / FALSE(boolean):是否启用调试以显示错误消息** passive ** TRUE TRUE / FALSE(布尔值):是否使用被动模式   

upload($locpath, $rempath[, $mode = 'auto'[, $permissions = NULL]])

参数:

$ locpath(string) - 本地文件路径$ rempath(string) - 远程文件路径$ mode(string) - FTP模式,默认为'auto'(选项为'auto','binary','ascii')$ permissions (int) - 文件权限(八进制)

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ locpath字符串) - 本地文件路径
  • $ rempath字符串) - 远程文件路径
  • $ mode字符串) - FTP模式,默认为'auto'(选项为:'auto','binary','ascii')
  • $ permissionsint) - 文件权限(八进制)
返回:成功时为TRUE,失败时为FALSE
Return type:  bool
将文件上传到您的服务器。您必须提供本地路径和远程路径,并且可以选择设置模式和权限。例:

$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);

如果使用“自动”模式,它将以源文件的文件扩展名为基础。

如果设置,权限必须作为八进制值传递。

download($rempath, $locpath[, $mode = 'auto'])

参数:

$ rempath(string) - 远程文件路径$ locpath(string) - 本地文件路径$ mode(string) - FTP模式,默认为'auto'(选项为'auto','binary','ascii')

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ rempath字符串) - 远程文件路径
  • $ locpath字符串) - 本地文件路径
  • $ mode字符串) - FTP模式,默认为'auto'(选项为:'auto','binary','ascii')
返回:成功时为TRUE,失败时为FALSE
Return type:  bool
从服务器下载文件。您必须提供远程路径和本地路径,您可以选择设置模式。例:

$this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');

如果使用“自动”模式,它将以源文件的文件扩展名为基础。

如果下载未成功执行(包括PHP没有写入本地文件的权限),则返回FALSE。

rename($old_file, $new_file[, $move = FALSE])

参数:

$ old_file(字符串) - 旧文件名$ new_file(字符串) - 新文件名$ move(bool) - 是否正在执行移动

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ old_file字符串) - 旧文件名
  • $ new_file字符串) - 新文件名
  • $ movebool) - 是否正在执行移动
Returns:  TRUE on success, FALSE on failure
Return type:  bool
允许您重命名文件。提供源文件名/路径和新文件名/路径。

//将green.html重命名为blue.html $ this-> ftp-> rename('/ public_html / foo / green.html','/public_html/foo/blue.html');

move($old_file, $new_file)

参数:

$ old_file(字符串) - 旧文件名$ new_file(字符串) - 新文件名

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ old_file字符串) - 旧文件名
  • $ new_file字符串) - 新文件名
Returns:  TRUE on success, FALSE on failure
Return type:  bool
让你移动一个文件。提供源和目标路径:

// Moves blog.html from "joe" to "fred" $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');

注意

如果目标文件名不同,则文件将被重命名。

delete_file($filepath)

参数:

$ filepath(字符串) - 要删除的文件的路径

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ filepath字符串) - 要删除的文件的路径
Returns:  TRUE on success, FALSE on failure
Return type:  bool
让你删除一个文件。使用文件名提供源路径。

$this->ftp->delete_file('/public_html/joe/blog.html');

delete_dir($filepath)

参数:

$ filepath(字符串) - 要删除的目录的路径

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ filepath字符串) - 要删除的目录的路径
Returns:  TRUE on success, FALSE on failure
Return type:  bool
允许您删除目录及其包含的所有内容。用尾部斜杠提供目录的源路径。

重要

用这种方法非常小心!它将递归地删除提供的路径中的所有内容,包括子文件夹和所有文件。确保你的路径是正确的。先尝试使用list_files()以验证您的路径是否正确。

$this->ftp->delete_dir('/public_html/path/to/folder/');

list_files([$path = '.'])

参数:

$ path(string) - 目录路径

返回:

文件的数组列表或失败时的FALSE

返回类型:

排列

  • $ pathstring) - 目录路径
返回:文件的数组列表或失败时的FALSE
Return type:  array
允许您检索服务器上以文件形式返回的文件列表。您必须提供所需目录的路径。

$list = $this->ftp->list_files('/public_html/'); print_r($list);

mirror($locpath, $rempath)

参数:

$ locpath(字符串) - 本地路径$ rempath(字符串) - 远程路径

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ locpath字符串) - 本地路径
  • $ rempathstring) - 远程路径
Returns:  TRUE on success, FALSE on failure
Return type:  bool
递归读取本地文件夹及其包含的所有内容(包括子文件夹),并基于此文件通过FTP创建镜像。无论在服务器上重新创建原始文件路径的目录结构。您必须提供源路径和目标路径:

$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');

mkdir($path[, $permissions = NULL])

参数:

$ path(string) - 创建$ permissions的目录路径(int) - 权限(八进制)

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ pathstring) - 要创建的目录的路径
  • $ permissionsint) - 权限(八进制)
Returns:  TRUE on success, FALSE on failure
Return type:  bool
让您在服务器上创建一个目录。提供以您希望创建的文件夹名称结尾的路径,并以斜杠结尾。

权限可以通过在第二个参数中传递八进制值来设置。

// Creates a folder named "bar" $this->ftp->mkdir('/public_html/foo/bar/', 0755);

chmod($path, $perm)

参数:

$ path(string) - 修改$ perm(int)权限的路径 - 权限(八进制)

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ pathstring) - 修改权限的路径
  • $ permint) - 权限(八进制)
Returns:  TRUE on success, FALSE on failure
Return type:  bool
Permits you to set file permissions. Supply the path to the file or directory you wish to alter permissions on:

// Chmod "bar" to 755 $this->ftp->chmod('/public_html/foo/bar/', 0755);

changedir($path[, $suppress_debug = FALSE])

参数:

$ path(string) - 目录路径$ suppress_debug(bool) - 是否关闭此命令的调试消息

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ pathstring) - 目录路径
  • $ suppress_debugbool) - 是否关闭此命令的调试消息
Returns:  TRUE on success, FALSE on failure
Return type:  bool
将当前工作目录更改为指定的路径。

$suppress_debug如果您想将此方法用作is_dir()FTP 的替代方法,该参数非常有用。

close()

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

FTP相关

Codeigniter 3

CodeIgniter 是一个PHP MVC框架,特点是超轻量级、有数据加密、有灵活URI路由等。对于 PHP 程序员来说,它小巧但功能强大。

主页 https://codeigniter.com/
源码 https://github.com/bcit-ci/CodeIgniter
版本 3
发布版本 3.1.5