Uploading files through FTP using PHP

Posted on

During developing web projects, we always need FTP Clients(FileZila, WinSCP, SmartFTP etc.) to send or upload files to a server via FTP. However,  if you are PHP developer, you can easily create your own FTP script to sending files from one server to another server. The PHP provides ftp_put function to upload a file to the FTP server. Below is a complete php ftp script to send files via FTP using php.


<?php
$sourcefile = ‘sendfile.txt’;
$dest_file = ‘readme.txt’;
$ftp_server=”;
$ftp_user_name=”;
$ftp_user_pass=”;
$ftp_con = ftp_connect($ftp_server);
$login_result = ftp_login($ftp_con, $ftp_user_name, $ftp_user_pass);
if (ftp_put($ftp_con, $dest_file, $sourcefile, FTP_ASCII)) {
echo “Successfully uploaded $file\n”;
} else {
echo “FTP upload Failed\n”;
}
ftp_close($conn_id);

Above script will first make connection to the ftp server using ftp_connect php function. Once the connection is created, you just need to pass ftp login details in ftp_login php function. Finally the ftp_put php function will perform file sending task.

Leave a Reply

Your email address will not be published. Required fields are marked *