Syntax:ftp_delete ($ftp_connection, $file)
Parameters:this function takes two parameters as specified above and described below:
- $ftp_connection:Required. It specifies a pre-existing FTP connection that will be used to execute FTP commands or functions.
- $file:Required. It specifies the path to the file on the server to be deleted.
Return Value:Returns TRUE on success or FALSE on failure.
Notes : - This function is available for PHP 4.0.0 and above.
- The following examples cannot be run in an online environment IDE. Therefore, try running the correct ftp server name, username and password on some PHP hosting server or localhost.
- ★ ★ ★ Make sure the file provided as a parameter for deletion also exists you have permission to be deleted by the ftp user logged into the FTP connection, otherwise it will throw an error.
Example :
// Connect to FTP server
// Assign ftp server to a variable
$ftp_server
=
"localhost"
;
// Use correct ftp username
$ftp_username
=
"user"
;
// Use correct FTP password matching
// to the ftp username
$ftp_userpass
=
"user"
;
// Filename or filename with path
// file on the server to be deleted
$file
=
"test.txt"
;
// Setting up 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!"
;
// Log in to an established connection
// ftp username and password
$login
= ftp_login ( $ftp_connection
,
$ftp_username
,
$ftp_userpass
);
if
(
$login
) {
// Success check login
echo
"
logged in successfully! "
;
// ftp_delete() function to delete a file from the FTP server
if
(ftp_delete (
$ftp_connection
,
$file
)) {
echo
"
deletion of"
.
$file
.
"is successful."
;
}
else
{
echo
"
Error while deleting the file"
.
$file
;
}
}
else
{
echo
"
login failed!"
;
}
// Close connection
if
(ftp_close (
$ftp_connection
)) {
echo
"
Connection closed Successfully!"
;
}
}
?>
Output:successfully connected to the ftp server! logged in successfully! deletion of ./htdocs/test.txt is successful. Connection closed Successfully!
If the file is deleted and run the same program again, provided that the file does not exist because it has already been deleted, an error will occur. the output will look likesuccessfully connected to the ftp server! logged in successfully! Error while deleting the file ./htdocs/test.txt Connection closed Successfully!
Link: https://www.php.net/manual/en/function.ftp-delete.php