【PHP ftp_delete()函数用法示例和解析】ftp_delete()函数是PHP中的内置函数, 用于删除FTP服务器上的文件。
语法如下:
ftp_delete( $ftp_connection, $file )
参数:此函数接受上述和以下所述的两个参数:
- $ftp_connection:它是必填参数。它指定用于执行FTP命令或功能的现有FTP连接。
- $file:它是必填参数。它指定要删除的服务器的文件路径。
注意:
- 此功能可用于PHP 4.0.0和更高版本。
- 以下示例无法在在线IDE上运行。因此, 请尝试使用适当的ftp服务器名称, 用户名和密码在某些PHP托管服务器或localhost中运行。
- ★★★确保作为参数删除提供的文件存在并且具有登录ftp连接的ftp用户的删除权限, 否则会产生错误。
<
?php//Connect to FTP server//Assign ftp server to the variable
$ftp_server = "localhost" ;
//Use correct ftp username
$ftp_username = "user" ;
//Use correct ftp password corresponding
//to the ftp username
$ftp_userpass = "user" ;
//Filename or filename with path to specify
//the file on server to be deleted
$file = "test.txt" ;
//Establishing ftp connection
$ftp_connection = ftp_connect( $ftp_server )
or die ( "Could not connect to $ftp_server" );
if ( $ftp_connection ) {
echo "successfully connected to the ftp server!" ;
//Logging in to established connection with
//ftp username and password
$login = ftp_login( $ftp_connection , $ftp_username , $ftp_userpass );
if ( $login ) { //Checking whether logged in successfully or not
echo "<
br>
logged in successfully!" ;
//ftp_delete() function to delete file from FTP server
if (ftp_delete( $ftp_connection , $file )) {
echo "<
br>
deletion of " . $file . " is successful." ;
}
else {
echo "<
br>
Error while deleting the file " . $file ;
}
}
else {
echo "<
br>
login failed!" ;
}//Closeingconnection
if (ftp_close( $ftp_connection )) {
echo "<
br>
Connection closed Successfully!" ;
}
}
?>
输出如下:
successfully connected to the ftp server!
logged in successfully!
deletion of ./htdocs/test.txt is successful.
Connection closed Successfully!
如果该文件被删除并再次运行同一程序, 但前提是该文件不存在(已删除), 因此会发生错误。输出看起来像
successfully connected to the ftp server!
logged in successfully!
Error while deleting the file ./htdocs/test.txt
Connection closed Successfully!
参考: https://www.php.net/manual/en/function.ftp-delete.php
推荐阅读
- OYO面试经验|S10(SDE校园内)
- 本文教你如何激活windows764位旗舰版系统
- 一键windows732位系统重装64位图文详细教程图解
- win764位旗舰版激活工具制作详细说明
- win7 64位旗舰版 激活系统图文详细教程图解
- windows7旗舰版32位激活工具图文详细教程图解
- 系统之家Ghost windows7纯净版64位旗舰版系统最新推
- 硬盘安装windows7 系统64位旗舰版图文详细教程图解
- ghost windows7 64位旗舰版系统激活密钥大全制作详细说明