views:

2220

answers:

3

Hey,

I'm trying to write a script that will upload the entire contents of a directory stored on my server to other servers via ftp.

I've been reading through the documentation on www.php.net, but can't seem to find a way to upload more then one file at a time.

Is there a way to do this, or is there a script that will index that directory and create an array of files to upload?

Thanks in advance for your help!

A: 

If you want to have multiple files uploaded at once, you'll need to use thread or fork.

I'm not sure of a Thread implentation in PHP, but you should take a look at the PHP SPL and/or PEAR

Edit: Thanks to Frank Farmer to let me know that there was a fork() function in PHP known as pcntl_fork()

You'll also have to get the whole content directory recursively to be able to upload all the file for a given directory.

Boris Guéry
PHP doesn't thread, but it does fork (using pcntl_fork()), and there are some thread-like wrappers for PHP's fork functionality.
Frank Farmer
thank you, for the tip, i added the link to my post.
Boris Guéry
I wouldn't recommend using forks. That's hairy stuff. It's far easier to simply open multiple connections and use a socket_select() to write to then asynchronously.
Sander Marechal
A: 

Do it in a loop, iterating through all the files in the folder

$servername = $GLOBALS["servername"];
    $ftpUser = $GLOBALS["ftpUser"];
    $ftpPass = $GLOBALS["ftpPass"];
$conn_id = ftp_connect($servername) or die("<p style=\"color:red\">Error connecting to $servername </p>");

if(ftp_login($conn_id, $ftpUser, $ftpPass))
{
    $dir_handle = @opendir($path) or die("Error opening $path");

         while ($file = readdir($dir_handle)) {
            ftp_put($conn_id, PATH_TO_REMOTE_FILE, $file)
        }
}
aquillin
+1  A: 

Once you have a connection open, uploading the contents of a directory serially is simple:

foreach (glob("/directory/to/upload/*.*") as $filename)
    ftp_put($ftp_stream, basename($filename) , $filename, FTP_BINARY);

Uploading all files in parallel would be more difficult.

Frank Farmer