During developing a web project, 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.
Also, read:
- 15+ regular expressions for PHP developers
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
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.
You may also like:
- Working with php.ini file Configuration
- Control Statements in PHP
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Using Prepared Statement with PHP & MySQL
- How to Upload File in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
- Manipulating PHP arrays: push, pop, shift, unshift
- Remove Repeated Words From String in PHP
- Converting a PHP Array to a Query String
- 15+ regular expressions for PHP developers
- 10 Most Important Directory Functions in PHP
- 10 little known but useful PHP functions
- PHP Script to Download Large Files Reliably