文字

ftp://

ftps://

ftp:// -- ftps://访问 FTP(s) URLs

说明

允许通过 FTP 读取存在的文件,以及创建新文件。 如果服务器不支持被动(passive)模式的 FTP,连接会失败。

打开文件后你既可以读也可以写,但是不能同时进行。 当远程文件已经存在于 ftp 服务器上,如果尝试打开并写入文件的时候, 未指定上下文(context)选项 overwrite,连接会失败。 如果要通过 FTP 覆盖存在的文件, 指定上下文(context)的 overwrite 选项来打开、写入。 另外可使用 FTP 扩展来代替。

如果你设置了 php.ini 中的 from 指令, 这个值会作为匿名(anonymous)ftp 的密码。

用法

  • ftp://example.com/pub/file.txt
  • ftp://user:password@example.com/pub/file.txt
  • ftps://example.com/pub/file.txt
  • ftps://user:password@example.com/pub/file.txt

可选项

封装协议概要
属性 PHP 4 PHP 5
受 allow_url_fopen 影响 Yes Yes
允许读取 Yes Yes
允许写入 Yes (仅支持新文件) Yes (新文件/启用 overwrite 后已存在的文件)
允许添加 No Yes
允许同时读和写 No No
支持 stat() No 自 5.0.0 起:仅仅 filesize() filetype() file_exists() is_file() is_dir() 。 自 PHP 5.1.0 起: filemtime()
支持 unlink() No Yes
支持 rename() No Yes
支持 mkdir() No Yes
支持 rmdir() No Yes

更新日志

版本 说明
4.3.0 增加 ftps://.

注释

Note:

FTPS 仅在 openssl 扩展开启时有效。

如果服务器不支持 SSL,这个连接会降级(falls back)到普通未加密的 ftp。

Note: 追加
自 PHP 5.0.0 起文件可以通过 ftp:// URL 封装器来追加(append)。 在之前的版本,尝试通过 ftp:// 来追加一个文件将会导致错误。

参见

  • FTP context options

用户评论:

[#1] fazil dot stormhammer dot nospam at gmail dot com [2008-04-25 13:41:17]

Document says "Allows read access to existing files and creation of new files via FTP. If the server does not support passive mode ftp, the connection will fail. "

As of version 5.2.5 at least fopen("ftp://...") uses an ACTIVE mode connection by default (it issues an FTP PORT command but not a PASV command).  To force passive mode:

$f = fopen("ftp://...");
ftp_pasv($f, true);

[#2] [2005-09-25 20:33:25]

<?php
$str 
="replace all contenents";
$filew="ftp://gufo:gufo@192.168.1.55:21/jj.php";
$opts = array('ftp' => array('overwrite' => true));
$context stream_context_create($opts);
$strwri file_put_contents($filew,$str,LOCK_EX,$context);
?>

[#3] php at f00n dot com [2004-07-04 00:39:21]

For Intranet purposes I found I preferred to move my file via ftp functions to match the session user's ftp account and put the file in a holding bay so I knew who it was from.

The FTP wrapper method will NOT do this if your ftp server does NOT support passive mode.

eg.  an ftp server behind NAT/routing

上一篇: 下一篇: