Inside this article we will understand i.e Upload File To Remote Server Through FTP Using PHP. Article contains classified information about uploading a file to remote server by connecting with FTP using PHP.
This tutorial will guide you only to upload a file. File can be of any type and extension. To upload file to remote server you need FTP details like hostname, username, password & filepath. You will understand the complete idea to upload a file into 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.
Read More: Password Generator with Strength Checker in JavaScript
Let’s get started.
PHP – FTP Functions
Here, we will see few PHP FTP functions and their use.
- ftp_connect()
- ftp_login()
- ftp_put()
- 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);
Upload a File To FTP Server
ftp_put($ftp_connection, $remote_file_name, $file_to_upload, FTP_ASCII)
Close a FTP Connection
ftp_close($ftp_connection)
Let’s create a complete PHP application by using all above functions.
Read More: How To Encrypt and Verify Password in PHP Tutorial
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
$file_to_upload = "yourfile.txt"; // File at application root
$remote_file = "";
// 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_put() function to upload a file to FTP server
if (ftp_put($ftp_connection, $remote_file, $file_to_upload, FTP_ASCII)) {
echo "Successfully!, file uploaded $file_to_upload\n";
} else {
echo "<br>Error while upload file " . $file;
}
} else {
echo "<br>login failed!";
}
// Closing connection
if (ftp_close($ftp_connection)) {
echo "<br>Connection closed Successfully!";
}
}
Application Testing
Open browser and type your application url.
URL: http://localhost/php-ftp/index.php
When application executes, it will upload your file to remote server.
Read More: Useful JavaScript console.log() Tricks Which You Need To Know
We hope this article helped you to learn How To Upload File To Remote Server Through FTP Using PHP Tutorial in a very detailed way.
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.
Read more