Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Send Files via FTP Using PHP

27 Responses »

Sending files to a server via FTP is an essential ability of any web developer or designer. Of course, we all use glossy FTP clients like WS_FTP and FireFTP, but what about FTP automation? You can use PHP to FTP files from one server to another. Let me show you how.

The PHP

$connection = ftp_connect($server);

$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);

if (!$connection || !$login) { die('Connection attempt failed!'); }

$upload = ftp_put($connection, $dest, $source, $mode);

if (!$upload) { echo 'FTP upload failed!'; }

ftp_close($connection); 

Obviously, the first task is to connect to the other server. Once the connection is created, you pass the login credentials. Once you're logged in, you can use the ftp_put() function to send the file. That's it!

What would you use this for? Well, you could set up a cron to create a database backup and send it to an offsite server.

Discussion

  1. October 10, 2008 @ 7:56 am

    hey!, you forgot to put the source code !!!

  2. October 10, 2008 @ 8:09 am

    @Alex: It’s there.

  3. October 10, 2008 @ 8:12 am

    great script !!!!
    Thanks!!!!

  4. October 10, 2008 @ 9:46 am

    What about using the FTP wrappers instead? The resulting code should be shorter and more intuitive. You could do the following directly and be done with it:

    $upload = copy($source, ‘ftp://user:password@host/path/file’);
    if (!$upload) { echo ‘FTP upload failed!’; }

  5. October 10, 2008 @ 11:34 am

    Cool :)

  6. jacob molin
    October 10, 2008 @ 12:24 pm

    $upload = copy($source,
    ‘ftp://user:password@host/path/file’);
    if (!$upload) { echo ‘FTP upload
    failed!’; }

    I think the way David Walsh do it is more secure.

    I don’t know but i think so ;)

  7. October 10, 2008 @ 1:13 pm

    I’d say they’re both as insecure. The work done behind the scenes for this particular snippet is the same. If you needed better control (e.g. chmod), however, you would have to use the ftp extension. But most of the time it’s not needed.

  8. October 12, 2008 @ 11:23 am

    Yay, I didn’t even know there was FTP functions in PHP.

    Could be useful to backup stuff… if it was more secure. Also, I think the script run time is limited (to 30 seconds in my case) on lots of hosts.

  9. October 12, 2008 @ 11:47 am

    The 30 seconds time-limit can in most cases be changed using
    set_time_limit(x);.

    If changed, it allows an execution time longer (or shorter…) than the default 30 secs.

  10. jacob molin
    October 12, 2008 @ 5:11 pm

    Yay, I didn’t even know there was FTP
    functions in PHP.

    Could be useful to backup stuff… if it
    was more secure. Also, I think the
    script run time is limited (to 30
    seconds in my case) on lots of hosts.

    Yes for MySQL BackUp i also use it together with a timer.

    But if i want to make a backup of other files on my ftp server i prefer cobian backup!
    Cobian
    In the start it was a professional programmer who made the program up to version 7 i think. Then he didn’t have time for it, and he published as opensource.
    Many other has then made the program up to version 9!

  11. October 19, 2008 @ 8:48 am

    great job =)

  12. mike
    October 26, 2008 @ 11:36 am

    And if you want to see progress ?

  13. November 2, 2008 @ 5:10 pm

    You can’t really see progress using PHP, that’d be more along the lines of Java or Perl. Well, truthfully, if you mess around with output buffering and the ftp nonblocking upload options, you can use ftp_nb_fput() or ftp_nb_put() along with ob_implicit_flush() to send out data via a while loop that updates the page. This can also be done via AJAX, if you flush your output buffer before uploading the file. At that point you can use an XMLHttpRequest to periodically (maybe every second) check on the file’s progress via another PHP file. I personally use Perl alongside the AJAX option though. If you do any of that though, watch out because you cannot force a header at that point, it all has to be done in the output. So it’d require multiple flushes of the output buffer.

  14. diego
    November 4, 2008 @ 5:11 am

    I get two problems:

    the script run time is limited
    I can’t see progress

    I would like to use this way to upload big files, rather than “post” way.

    EDIT: to solve the run time troubble, you should write “set_time_limit(0)” at the begin of the script (I don’t know if this could be insecure)

  15. abhinav joshi
    January 9, 2009 @ 7:08 am

    Cool :)

  16. February 23, 2009 @ 7:35 am

    Got more ideas, but can we restrict user to send only .doc file

  17. trink
    February 28, 2009 @ 8:57 pm

    Yes.
    Before $upload = ftp_put($connection, $dest, $source, $mode); put this:
    if(substr($dest,-4) != ‘.doc’) die(“You may only upload .doc files.”);
    Obviously the die() can be replaced with your own error handler, just ensure that it halts the script, or just put the rest in an else block.

  18. June 22, 2009 @ 12:13 am

    i did as you said but then i found this “Warning: ftp_put() [function.ftp-put]: Opening data channel for file transfer. in *“. can you explain why ?

  19. disha vaghela
    November 3, 2009 @ 4:28 am

    Hey David…its really helpful ! thnx a lot !!!

  20. blackbird
    November 11, 2009 @ 10:20 am

    @david: explain me below code and which are needed to replace with actual value.

    $upload = ftp_put($connection, $dest, $source, $mode);

    eg: $dest=..
    $source=..
    $mode=…

  21. February 25, 2010 @ 2:34 pm

    Thanks forever web pages..

  22. tushar
    March 4, 2010 @ 1:38 am

    cool script
    q :-
    i want to get email id from any page if exist?
    how i do it ?
    help me

  23. March 29, 2010 @ 3:48 pm

    i want to get email id from any page if exist?
    how i do it ?

  24. April 7, 2010 @ 3:25 am

    Nice article and insight. Good to have bumped onto your blog and met you.

  25. April 8, 2010 @ 12:47 pm

    goood insight what is want to get mail as attactment

  26. murthy
    June 28, 2010 @ 12:59 pm

    GOOOOD one i like it

  27. vinoth
    August 28, 2010 @ 11:26 pm

    thank you guys!!!!!!!!!!

    i got error on this code. :(

    after spending little time, i got the solution.

    ftp_pasv($connection, true); // to turns on the passive mode

    works!

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!