downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003


(PHP 3>= 3.0.13, PHP 4 )

ftp_put -- Uploads a file to the FTP server


bool ftp_put ( resource ftp_stream, string remote_file, string local_file, int mode [, int startpos])

ftp_put() stores local_file on the FTP server, as remote_file. The transfer mode specified must be either FTP_ASCII or FTP_BINARY.

Note: The startpos parameter was added in PHP 4.3.0.

Returns TRUE on success or FALSE on failure.

Example 1. ftp_put() example

$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII); 

See also ftp_fput(), ftp_nb_fput(), and ftp_nb_put().

add a note add a note User Contributed Notes
24-Sep-2003 11:43
if you examine the first user submitted function, ftp_putAll, it will work only if you extract this line and its matching bracket.

if (!@ftp_chdir($conn_id, $dst_dir."/".$file))

The function will have changed into that directory before having uploaded files to it.  This alters your upload path and the system will try to upload into an essentially non-existent directory (duped at the end).

Hope this helps some of you.
lucas at rufy dot com
04-Aug-2003 03:45
The following is a fully tested function (based on a previous note) that recursively puts files from a source directory to a destination directory. See for more information.

NOTE: use full path name for the destination directory and the destination directory must already exist

function ftp_putAll($conn_id, $src_dir, $dst_dir) {
    $d = dir($src_dir);
    while($file = $d->read()) { // do this for each file in the directory
        if ($file != "." && $file != "..") { // to prevent an infinite loop
            if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
                if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {
                    ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
                ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
            } else {
                $upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
thbird9 at hotmail dot com
24-Mar-2003 04:51
if you use the script from "kiwo1 at yahoo dot com", you can find out the original filename with:

$name = $source_file_name;
darian lassan at yahoo de
05-Feb-2003 08:06
ftp_put() overwrites existing files.
Again: trivial but not mentioned here.
darian lassan at yahoo de
05-Feb-2003 02:03
You get a timeout using ftp_put()?

Maybe active ftp is blocked by a firewall. In this case try using ftp_pasv($ftp_con,TRUE) to turn on passive ftp directly after ftp_login().
mailinglist at genialsoft dot be
15-Jan-2003 08:27
I just try to upload some file with ftp_put and I always received the following error :
"Cannot STOR. Cannot change Port if not closed"

The solution is to set the passive mode on with ftp_pasv($conn_id, true);
victor59hk at yahoo dot com dot hk
02-Jan-2003 06:32
ftp_set_option($this->ftp, FTP_AUTOSEEK, FALSE);
$fname = 'abc.txt';
ftp_put($this->ftp, $fname, $src, FTP_BINARY, $this->ftp_size($fname));

content's of "abc.txt":

content's of $src:

the script above can perform an "APPEND" operation
that mean the final content of "abc.txt" is "ABCDEF"
webmaster at lugosoft dot com
18-Dec-2002 01:53
$fmode  = "FTP_ASCII";

Long story:
The parameter 4 referred to is the mode, which is a long; FTP_ASCII is just a constant for that number. But if you put it in quotes, PHP tries to translate it to a long... hence the warning.

Short story:
Change the above line to:
$fmode  = FTP_ASCII;

... and it should work fine. Tested on Linux, PHP 4.2.3.
oheil at ecc-gmbh dot de
25-Sep-2002 07:43
If you want to copy a whole directory tree (with subdiretories),
this function (ftp_copy) might be usefull. Tested with
php 4.2.2 and a Linux OS.

$conn_id = ftp_connect("server_adress");

$src_dir = "/from";
$dst_dir = "/to";

ftp_copy($src_dir, $dst_dir);

Function: ftp_copy()
function ftp_copy($src_dir, $dst_dir) {

global $conn_id;

$d = dir($src_dir);

    while($file = $d->read()) {

        if ($file != "." && $file != "..") {

            if (is_dir($src_dir."/".$file)) {

                if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {

                ftp_mkdir($conn_id, $dst_dir."/".$file);

            ftp_copy($src_dir."/".$file, $dst_dir."/".$file);
            else {

            $upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY);

kiwo1 at yahoo dot com
29-Aug-2002 12:05
If you wanna upload files from your harddisk by a form to a specified ftp this sample can help you...
First of all create the form:

<body marginwidth=4 marginheight=4 topmargin=4 leftmargin=4 bgcolor=white vlink="#0000ff" link="#0000ff">

<form name="Attachments"  method=POST action="sendimage.php" enctype="multipart/form-data">

<input type=hidden name=box value="">

<td nowrap width="1%">&nbsp;&nbsp;<b>Image:</b></td>
<td colspan=2>
<input type=file name=source_file size=20> <br>


<input type=submit name=btnSubmit value=Submit size=20 style="border: 1px solid #0000FF"></form>

The critical point in this form is the usage of  enctype="multipart/form-data"
If you don't use this part your upload operations won't work.
Then u must create sendimage.php as follows:

$conn_id ftp_connect($ftp_server); 
// login with username and password
$login_result ftp_login($conn_id$user$passwd); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
"FTP connection has failed!";
"Attempted to connect to $ftp_server for user $ftp_user_name"
    } else {
"<br>Connected to $ftp_server, for user $user<br>";
//directorylike /


echo (

echo (

// upload the file
$upload ftp_put($conn_id$destination_file$source_fileFTP_BINARY); 

// check upload status
if (!$upload) { 
"FTP upload has failed!";
    } else {
"Uploaded $source_file to $ftp_server as $destination_file";

// close the FTP stream

In this example code $source_file is the path of the file in your disk, and destination file is the name of the uploaded file in ftpserver.
In this code I use ftp_chdir to give the path of the
 uploaded file within ftpserver..
For your questions about all categories of PHP my
c u...
victor at nobel dot com dot br
16-Feb-2002 05:28
to correct use of function ftp_put, use your public_html in place of /home/USER/public_html, like this:

$dir = "/home/hup/public_html/upload"; // --> ERROR


    $dirOK = "/public_html/upload/";
    $fp = @ftp_connect("localhost");
    $login = @ftp_login ($fp, "$USERNAME", "$PASSWD");
    @ftp_chdir($fp, $pasta);
    @ftp_put($fp, $arquivo_name, $dirOK$arquivo, FTP_BINARY);
clambert at whitecrown dot net
30-Jun-2001 09:24
When using versions of PHP below 4.04b4, the ftp_put command doesn't work on NT/IIS4. Most of the other functions will work, but there was a bug while trying to send upstream data to an IIS webserver.
Paul Southworth
16-Mar-2000 01:34
My stupid error: if you put the mode in quotes it will silently default to ASCII mode.  That is to say:

ftp_put( $ftp, $a, $b, "FTP_BINARY");

[ED: This behaviour is due to the fact PHP thinks FTP_BINARY is a string and converts it to an integer which is not the same as FTP_BINARY which is just a symbolic constant]

 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST