PHP 8.3.4 Released!

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

可选项

封装协议概要
属性 是否支持
allow_url_fopen 影响
允许读取
允许写入 是 (新文件/启用 overwrite 后已存在的文件)
允许添加
允许同时读和写
支持 stat() filesize()filemtime()filetype()file_exists()is_file()is_dir()
支持 unlink()
支持 rename()
支持 mkdir()
支持 rmdir()

注释

注意:

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

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

注意: 追加
文件可以通过 ftp:// URL 封装器来追加(append)。

add a note

User Contributed Notes 3 notes

up
-8
php at f00n dot com
19 years ago
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
up
-14
Anonymous
18 years ago
<?
$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);
?>
up
-15
fazil dot stormhammer dot nospam at gmail dot com
15 years ago
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);
To Top