Skip to main content

Upload File with FTP using PHP

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);

Uploading File:
ftp_put($con, $remote_file, $local_file, FTP_ASCII);

Closing Connection:
ftp_close($con);

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);

Comments

  1. hi...

    i want to ask,how to upload multiple file via ftp using php?I take a lot of time in searching it....there is answer that we can use "multiple" but i don't know how to do the looping process..
    Hope you can help me....

    ReplyDelete
    Replies
    1. Thanks for your query! I hope this code will help you...

      $files = array(
      'local/1.txt' => 'remote/1.txt',
      'local/2.txt' => 'remote/3.txt',
      'local/3.txt' => 'remote/3.txt');

      foreach($files as $local => $remote){
      $upload = ftp_put($con, $remote, $local, FTP_ASCII);
      }

      Delete
    2. Thanks for your code...but,i don't know how to implement it in my coding.Can you help me by giving full example of codes that include html and php part...

      Delete
    3. only PHP:

      http://pastebin.com/CC3xLG01

      Delete
    4. thanks...but i don't understand the array part which is:

      'path/to/local/file1.txt' => 'path/to/remote/file1.txt'

      is it i have to write the path for the file to upload?

      Delete
    5. sorry,but can you give more clearer explanation on the codes that you give since i have took a lot of time on think about this.

      Delete
    6. Only for you full and final! 100% working! tested ;)

      http://paste.laravel.com/192H

      Delete
    7. thanks for your code...but i still have problem where error like this "ftp_put() [function.ftp-put]: The parameter is incorrect."...What's wrong?
      And also my ftp account is not localhost and have its username and password...i thought that we have to use the $ftp_server,$ftp_user_name,$ftp_user_pass??

      Then,my upload directory is at Local Disk (C)...
      What i've to change for this code??help me...
      Sorry,for make you trouble...

      Delete
  2. Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.
    php jobs in hyderabad.

    ReplyDelete
  3. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingAzure Online course hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery. Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar.We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Force File Download with PHP

If you want to control or track your downloadable contents then a force file downloader script can give you more option. Especially if you want make images or other stemming contents like music file (mp3, wav, ogg), video files (flv, mp4, ogg, 3gpp), css, JavaScripts or even a php file downloadable then you must need to use a force file downloader script.
In this post I am going to share a simple but useful force file downloader script.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin. It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.