其他服务--Ftp

2021-03-24

要使用这些FTP相关函数,在编译的时候请添加 --enable-ftp 选项。
从PHP 7.0.0开始的 Windows 版本,默认以动态的方式载入该扩展,使用前需要在php.ini中开启该扩展。

建立/关闭连接

//建立一个FTP连接到指定的服务器host
ftp_connect(string $host , int $port = 21 , int $timeout = 90):resource
	host 要连接的服务器地址
	port 为要连接到的 FTP 器的端口号,如果没有设置或者为 0,则会使用默认的端口 21 来连接
	timeout 用来设置网络传输的超时时间限制。如果此选项留空,则默认的值为 90 秒。

//打开SSL-FTP 连接,即使服务器未配置 SSL-FTP,或者服务器的证书无效,ftp_ssl_connect() 函数也会成功的建立到服务器的连接。直到调用ftp_login()函数的时候, 客户端才会发送对应的 AUTH FTP 命令,此时,如果服务器未配置 SSL-FTP或者整数无效,ftp_login()函数会失败
ftp_ssl_connect(string $host , int $port = 21 , int $timeout = 90):resource

//关闭给出的连接标识符并释放资源
ftp_close(resource $ftp_stream):bool
ftp_quit(resource $ftp_stream):bool

登陆

//使用用户名和密码登录入给定的 FTP 连接
ftp_login(resource $ftp_stream , string $username , string $password):bool

设置

//设置FTP 流的各种运行时选项
ftp_set_option(resource $ftp_stream , int $option , mixed $value):bool
	option 
		FTP_TIMEOUT_SEC	改变网络传输的超时时间。参数 value 必须为整数且大于 0。默认的超时时间为 90 秒。
		FTP_AUTOSEEK	当此选项打开时,带 resumepos 或 startpos 参数的GET 或 PUT 请求 将先检索到文件中指定的位置。此选项默认是打开的。
		FTP_USEPASVADDRESS	当此选项禁用时,PHP 会忽略掉 FTP 服务器通过 PASV 命令返回的 IP 地址,直接使用在 ftp_connect() 中指定的地址。value 参数必须是布尔型

//为要上传的文件分配空间
ftp_alloc(resource $ftp_stream , int $filesize , string &$result = ?):bool
	ftp_stream FTP 连接标示符。
	filesize 要分配的空间,以字节为单位。
	result 如果提供此参数,那么服务器的响应 会以文本方式设置到 result 中

//返回当前 FTP 被动模式是否打开,只能在 FTP 登录成功后方可调用,否则会失败
ftp_pasv(resource $ftp_stream , bool $pasv):bool
	pasv 如果参数 pasv 为 true,打开被动模式传输 (PASV MODE) ,否则则关闭被动传输模式

发送命令

//在 FTP 服务器运行指定的命令
ftp_exec(resource $ftp_stream , string $command):bool

//向 FTP 服务器发送任意 command
ftp_raw(resource $ftp_stream , string $command):array

//向 FTP 服务器发送指定的命令,SITE 命令是非标准化的,不同的服务器不尽相同。主要用于处理文件权限以及组成员等事情
ftp_site(resource $ftp_stream , string $command):bool
	command SITE 命令,注意本参数没有经过处理,在文件名有存在空格或其它特殊字符的情况下可能会有问题

上传

//将文件内容追加到 FTP 服务器上的指定文件
ftp_append(resource $ftp , string $remote_filename , string $local_filename , int $mode = FTP_BINARY):bool

//上传文件到 FTP 服务器
ftp_put(resource $ftp_stream , string $remote_file , string $local_file , int $mode = FTP_BINARY , int $startpos = 0):bool
	ftp_stream FTP 连接资源。
	remote_file 远程文件路径。
	local_file 本地文件路径。
	mode 传送模式,只能为 FTP_ASCII(文本模式)或 FTP_BINARY(二进制模式)。
	startpos 指定开始上传的位置,一般用来文件续传

//上传一个已经打开的文件到 FTP 服务器
ftp_fput(resource $ftp_stream , string $remote_file , resource $handle , int $mode , int $startpos = 0):bool
	ftp_stream FTP 连接的链接标识符。
	remote_file 远程文件路径。
	handle 打开的本地文件句柄,读取到文件末尾。
	mode 传输模式只能为 (文本模式) FTP_ASCII 或 (二进制模式) FTP_BINARY 其中的一个。
	startpos 远程文件上传的开始位置

//将文件存储到 FTP 服务器 (非阻塞),是异步上传文件,返回 FTP_FAILED 或 FTP_FINISHED 或 FTP_MOREDATA
ftp_nb_fput(resource $ftp_stream , string $remote_file , resource $handle , int $mode = FTP_IMAGE , int $startpos = 0):int
	ftp_stream FTP 连接标示符。
	remote_file 远程文件路径。
	handle 已经打开的本地文件指针,当读取到文件末尾时结束。
	mode 传输模式。必须是 FTP_ASCII 或 FTP_BINARY。
	startpos 要将文件存储到远程文件的开始位置(即从远程文件的哪个字节位置开始存储)

//把本地文件 local_file 存储到 FTP 服务器上由 remote_file 参数指定的路径,采用的是异步传输模式;返回 FTP_FAILED 或 FTP_FINISHED 或 FTP_MOREDATA
ftp_nb_put(resource $ftp_stream , string $remote_file , string $local_file , int $mode = FTP_BINARY , int $startpos = 0 ):int
	ftp_stream FTP 连接的链接标识符。
	remote_file 远程文件路径。
	local_file 本地文件路径。
	mode 传输模式选择,可选参数为 FTP_ASCII(文本模式)或 FTP_BINARY(二进制模式)。
	startpos 指定传输开始的位置,用来续传支持

下载

//用来下载由 remote_file 指定的文件,并写入到本地已经被打开的一个文件中
ftp_fget(resource $ftp_stream , resource $handle , string $remote_file , int $mode , int $resumepos = 0):bool
	ftp_stream FTP 连接的链接标识符。
	handle 本地已经打开的文件的句柄。
	remote_file 远程文件的路径。
	mode 传送模式参数, 必须是 (文本模式) FTP_ASCII 或 (二进制模式) FTP_BINARY 中的一个。
	resumepos 远程文件开始下载的位置。

//下载 FTP 服务器上指定的文件并保存为本地文件
ftp_get(resource $ftp_stream , string $local_file , string $remote_file , int $mode , int $resumepos = 0):bool
	ftp_streamFTP 连接的链接标识符。
	local_file 文件本地的路径(如果文件已经存在,则会被覆盖)。
	remote_file 文件的远程路径。
	mode 传送模式。只能为 (文本模式) FTP_ASCII 或 (二进制模式) FTP_BINARY 中的其中一个。
	resumepos 从远程文件的这个位置继续下载

//从 FTP 服务器获取文件并写入到一个打开的文件(非阻塞),是异步方式获取文件的,返回 FTP_FAILED 或 FTP_FINISHED 或 FTP_MOREDATA
ftp_nb_fget(resource $ftp_stream , resource $handle , string $remote_file , int $mode = FTP_IMAGE , int $resumepos = 0):int
	ftp_stream FTP 连接标示符。
	handle 用来存储数据的一个已经打开的文件句柄。
	remote_file 远程文件路径。
	mode 传输模式,必须是 FTP_ASCII 或者 FTP_BINARY。
	resumepos 远程文件开始下载的位置(即从远程文件的哪个字节开始下载)

//从 FTP 服务器上获取文件并写入本地文件,是通过异步的方式来获取文件,返回 FTP_FAILED 或 FTP_FINISHED 或 FTP_MOREDATA
ftp_nb_get(resource $ftp_stream , string $local_file , string $remote_file , int $mode = FTP_BINARY , int $resumepos = 0):int
	ftp_stream FTP 连接句柄。
	local_file 保存到的本地文件路径(如果文件已存在会被覆盖)。
	remote_file 远程文件路径。
	mode 指定传输模式。必须是 FTP_ASCII 或 FTP_BINARY。
	resumepos 开始下载文件的起始位置

断点续传

//以不分块的方式,连续获取/发送文件;返回 FTP_FAILED 或 FTP_FINISHED 或 FTP_MOREDATA
ftp_nb_continue(resource $ftp_stream):int

获取目录/文件信息

//返回当前目录名
ftp_pwd(resource $ftp_stream):string

//返回指定目录中的文件列表,如果错误,则返回 false
ftp_mlsd(resource $ftp , string $directory):array|false

//返回给定目录的文件列表,失败否则返回 false
ftp_nlist(resource $ftp_stream , string $directory):array

//返回指定目录下文件的详细列表
ftp_rawlist(resource $ftp_stream , string $directory , bool $recursive = false):array

//返回指定文件的大小(以字节为单位)
ftp_size(resource $ftp , string $filename):int

//获取远程文件的最后修改时间(UNIX 时间戳),发生错误或文件不存在则返回 -1
ftp_mdtm(resource $ftp_stream , string $remote_file):int

目录/文件操作

//在 FTP 服务器上,创建指定的目录 directory,发生错误时返回 false
ftp_mkdir(resource $ftp_stream , string $directory):string

//删除 FTP 服务器上 directory 参数指定的目录
ftp_rmdir(resource $ftp , string $directory):bool

//删除 FTP 服务器上的一个由参数 path 指定的的文件
ftp_delete(resource $ftp_stream , string $path):bool

//更改 FTP 服务器上的文件或目录名
ftp_rename(resource $ftp_stream , string $oldname , string $newname):bool

//切换目录至当前目录的父目录 (上级目录)
ftp_cdup(resource $ftp_stream):bool

//将当前目录切换为指定的目录
ftp_chdir(resource $ftp_stream , string $directory):bool

//将服务器上的文件权限设置为 mode 指定的值
ftp_chmod(resource $ftp_stream , int $mode , string $filename):int

获取系统信息

//返回远程 FTP 服务器的操作系统类型,发生错误则返回 false。
ftp_systype(resource $ftp_stream):string

获取设置信息

//返回连接句柄为 ftp_stream,指定键值 option 的值
ftp_get_option(resource $ftp_stream , int $option):mixed
	option
		FTP_TIMEOUT_SEC	返回当前设定的网络操作的超时时间。
		FTP_AUTOSEEK	此选项打开时返回 true,否则返回 false。

 

{/if}