Inside this article we will understand in PHP How to Delete a file using FTP from remote server. Article contains classified information about deleting a file from remote server by connecting with FTP.
This tutorial will guide you only to delete a file. File can be of any type and extension. To delete file from remote server you need FTP details like hostname, username, password & filepath. You will understand the complete idea to delete file from remote server using PHP.
PHP provides several functions for FTP. We will use those functions and work.
The File Transfer Protocol is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network.
Learn More –
- Copy Entire Contents of a Directory to Another Directory in PHP
- Create and Save XML File using DOMDocument in PHP
- Encrypt Form Data in JavaScript Before Sending to Server
- Find and Count All Headings From a Web Page in PHP
Let’s get started.
PHP – FTP Functions
Here, we will see few PHP FTP functions and their use.
- ftp_connect()
- ftp_login()
- ftp_delete()
- ftp_close()
To Establish FTP Connection
$ftp_connection = ftp_connect($ftp_server)
Login To FTP Server
$login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass);
Delete a File From FTP Server
ftp_delete($ftp_connection, $file)
Close a FTP Connection
ftp_close($ftp_connection)
Let’s create a complete PHP application by using all above functions.
Create an Application
Create a folder php-ftp in your localhost directory.
Inside this folder, create a file called index.php. Open index.php and write this complete code into it.
<?php // Connect to FTP server // Assign ftp server to the variable $ftp_server = "ftp.xxxxxxx.com"; // Use correct ftp username $ftp_username = "yyyyy"; // Use correct ftp password corresponding // to the ftp username $ftp_userpass = "zzzzz"; // Filename or filename with path to specify // the file on server to be deleted $file = "/public_html/yourfolder/yourfile.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!"; } // Closing connection if (ftp_close($ftp_connection)) { echo "<br>Connection closed Successfully!"; } }
Application Testing
Open browser and hit this URL: http://localhost/php-ftp
When application executes, it will delete your remote file.
We hope this article helped you to learn PHP How To Delete a File Using FTP From Remote Server Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.