If you get this error when trying to send data to server :Warning: ftp_put() [function.ftp-put]: Unable to build data connection: Connection timed out...

Two solutions : - Add the program httpd.exe in your exception list for external connexions of your firewall. Indeed, the FTP protocol open a new socket for data transfer. And this socket is opened from the server to the client (your computer). This program is located (for WAMP) in C:\wamp\bin\apache\Apache[version]\bin\ - Use the ftp_pasv() function to activate the passive mode. In this mode, it is the client who open the new socket to the server.

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 http://rufy.com/tech/archives/000026.html for more information.

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

I [had an error for which] ftp_pasv didnt solve the problem. Here's why:

FTP uses 2 connections on different ports, one for connection/handshake and another for data transfer.
The problem was that the ftp-server (that php was connecting to) also used different IP-addresses for the different connections (say what!?).
Normally the firewall (csf) detects ftp-connections and allows them through but because of the different IP-adresses this didn't work.

Solution:
1 angry mail to the ftp server owner.
Allowing the second IP in the firewall.

Here is a fix for the function from lucas at rufy dot com below that will recursively put files from a source directory to a destination directory. As written below, it won't put a file in a directory that already exists, because the the destination is altered. So here is the corrected function that will allow it to work:

victor at nobel dot com dot br wrote thatthe correct dirpath format excluded "/home/USER/" from the public path, but for my server, i had to use it in order to get my scripts to work.

it may be obvious to most but I'm positing that you cannot use the $_SERVER['DOCUMENT_ROOT'] path since FTP starts at your top-level and therefore bypasses (or just plain doesn't recognize) most of the virtual server pathing.

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

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("<span style='color:#FF0000'><h2>You do not have access to this ftp server!</h2></span>"); // login with username and password, or give invalid user message

I had a little trouble getting the ftp_put to work, because of that particular server. All variables and data parsed from the previous web form had to be retreived using $_POST, $_GET or $_FILES.

If you don't know what you sent use phpinfo(); to display what the server thinks about your data.

so...when sending files using a form and PHP, make sure that all the data (text files etc...) are retreived with $_POST, and files (smiley.png, compression.zip, etc...) are retreived with $_FILES.

here's what your start of a results.php file might look like:<?PHP $myName = $_POST['name']; //This will copy the text into a variable$myFile = $_FILES['file_name']; // This will make an array out of the file information that was stored.?>

Now when it comes to transmitting that information...

<?PHP $destination_path = "src/bin/";

//where you want to throw the file on the webserver (relative to your login dir)

$destination_file = $destination_path."img.jpg";

//This will create a full path with the file on the end for you to use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.

$file = $myFile['tmp_name'];

//Converts the array into a new string containing the path name on the server where your file is.

If you are moving files from one folder to another inside the same server, the "local file" field has to be indicated in a relative path according to the location of the script running the ftp_put() function.

For example, your function is running on: /public_html/do_ftp.php and you want to move /public_html/products.php to /public_html/backup/products.php

After having headaches for 2 days trying to make this function work using absolute paths in both fields, I finally found the right way to use it. I hope it helps someone. Excuse my english, it isn't my native language.

I've seen two notes about a "ftp_copy" function but i think there's a misinterpretation about what an "ftp_copy" function should do. For me , it should be something like an ftp_rename that would keep the orginal file and clone it somewhere else on the same ftp server, as for them they consider its purpose is to copy a local file to a distant ftp ..well .. as in FTP protocol there's no such thing as an FTP COPY command anyway, i think you're free to interpret it as you want.
So here's my solution using ftp_put and ftp_get ..

Currently, there is no function that lets you specifiy the file's contents as a string. However, there is ftp_fput(), which operates on an open file. Using this function in conjunction with tmpfile() lets you emulate this kind of function. (You could also use php://memory, but this breaks BC).

This is an extremely trivial thing but one that had me stumped forever (well, until I decided to check the error logs and see the error). I had a large file (mysql backup of a huge forum) that was only partially being uploaded to a remote backup server. Couldn't figure out why, until I realized that the max execution time was being hit since it was taking longer than 30 seconds to upload this file.

<?phpset_time_limit(0);rest of the code here....?>

That did the trick. It's one of those dumb, trivial things, and if you're having trouble like I, it may be something you overlooked.

I'm copying fairly large backup files from server to server. ftp_put was running fine for awhile until it occasionally began reporting errors.When I set TRUE as the value for the ftp_pasv () (after login), ftp_put started working again.

... and have tried ftp_pasv, ftp_alloc, switching between FTP_ASCII or FTP_BINARY and blaming the FTP provider (like I did), try using ftp_close then ftp_connect to reset your FTP connection. Worked for me!

This solution to a common problem is implied elsewhere, but I thought it might be useful to put it all in one place (since I spent hours piecing it together!)

Sometimes a web host will open PHP sessions with a user of 'nobody'. Files created by this user may not have the correct permissions to allow management of those files by the actual owner of the site. The following script allows the actual owner to open access to a directory so that 'nobody' can create a file using fopen(). Then using the handle created by 'nobody', the ftp_fput() command saves the file with the correct owner. The file 'nobody' created is discarded.<? $connection = ftp_connect($ftpServer);ftp_login($connection, $ftpUser, $ftpPass);ftp_chdir($connection, $ftpDir);// open the directory so that 'nobody' can create a temporary file ftp_site($cn, "CHMOD 777 $ftpDir");$new="tempFile"; @unlink($new); // just in case$handle=fopen($new,"x");chmod($new,0777);fputs($handle,"a bunch of stuff...");fclose($handle); // have to rewind $handle=fopen("tempFile","r");ftp_fput($connection, "finalFile", $handle, FTP_ASCII);fclose($handle);unlink($new);// remove open access to the directoryftp_site($connection, "CHMOD 755 $ftpDir")ftp_close($connection);?>

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 email:kiwo1@yahoo.comc u...