FTP is one of the most useful tool or protocol for all web developers or designers. We can move files from one server to another with FTP tools very easily. PHP provides build in support for FTP protocol. That’s why we can work with FTP very easily using some FTP functions. In this post I’m going to show how to upload or put file to a server using PHP functions.
Connecting to FTP Server:
$con = ftp_connect($host, 21);
Login to FTP Server:
ftp_login($con, $username, $password);
ftp_put($con, $remote_file, $local_file, FTP_ASCII);
Bring All Together:
$host = "localhost"; $username = "username"; $password = "password"; $local_file = 'path/to/file.txt'; $remote_file = 'public_html/file.txt'; $con = ftp_connect($host, 21) or die("Could not connect to FTP server"); $log = ftp_login($con, $username, $password) or die("Fail to longin"); $upload = ftp_put($con, $remote_file, $local_file, FTP_ASCII); if($upload) echo 'Success!'; ftp_close($con);