Uploading files through FTP using PHP

September 26th, 2012 by laeeq | 1 comment

As a web developer, we often need FTP Clients(FileZila, WinSCP, SmartFTP etc.) to send or upload files to a server via FTP. But if you are familiar with php, you can easily create your own FTP script to sending files from one server to another. 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.

  1. <?php
  2. $sourcefile = ‘sendfile.txt’;
  3. $dest_file = ‘readme.txt’;
  4. $ftp_server=;
  5. $ftp_user_name=;
  6. $ftp_user_pass=;
  7. $ftp_con = ftp_connect($ftp_server);
  8. $login_result = ftp_login($ftp_con, $ftp_user_name, $ftp_user_pass);
  9. if (ftp_put($ftp_con, $dest_file, $sourcefile, FTP_ASCII)) {
  10. echo “Successfully uploaded $file\n”;
  11. else {
  12. echo “FTP upload Failed\n”;
  13. }
  14. ftp_close($conn_id);
  15. ?>

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 can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Wrapping Text using PHP
  • PHP 5.5 beta 4 is now available
  • Swapping Array Keys and Values
  • PHP Security
  • Working with Image Metadata in PHP
  • Converting Between Relative and Absolute File Path
  • Parsing File Paths with PHP
  • How to remove HTML Comments with PHP?
  • How to access .htaccess values?
  • Dealing With Common PHP Errors
  • Interview Questions and Answers for Freshers
  • Display numbers with ordinal suffix using PHP
  • Function to set COLLATION on database fields of Mysql
  • Read and write to remote files using PHP
  • Sharing PHP SESSION Variables between Multiple Subdomains
  • PHP 5.5.0 Alpha4 Development Preview Released
  • Check a string starts with http using PHP
  • Cross-Site Scripting Attacks (XSS)
  • PHP 5.4.11 and PHP 5.3.21 released!
  • PHP Memory Leak Issue
  •  

     

    1. Jitendra
      October 10th, 2012 at 12:55 | #1

      Really nice tutorial.

    1. No trackbacks yet.