Skip to content
PHP Logo

How can PHP functions such as ftp_delete() and unlink() be utilized effectively for file management tasks?

To effectively manage files using PHP functions like ftp_delete() and unlink(), you can use ftp_delete() to remove files from an FTP server and unlink() to delete files from the local server. These functions can be used to clean up unnecessary files, manage file storage, or automate file deletion tasks.

php
// Example of using ftp_delete() to remove a file from an FTP server
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$file_to_delete = 'example.txt';

// Connect to FTP server
$ftp_conn = ftp_connect($ftp_server);
ftp_login($ftp_conn, $ftp_username, $ftp_password);

// Delete file from FTP server
if (ftp_delete($ftp_conn, $file_to_delete)) {
    echo "File $file_to_delete deleted successfully.";
} else {
    echo "Error deleting file $file_to_delete.";
}

// Close FTP connection
ftp_close($ftp_conn);

// Example of using unlink() to delete a file from the local server
$file_to_delete = 'example.txt';

// Delete file from local server
if (unlink($file_to_delete)) {
    echo "File $file_to_delete deleted successfully.";
} else {
    echo "Error deleting file $file_to_delete.";
}