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