tags:

views:

57

answers:

2

What is the best way using PHP and Curl to POST an entire folder to another server.

A: 

you can:

  • post all files in the directory consequently

  • zip the directory and post the archive

kgb
I think you mean consecutively http://www.merriam-webster.com/netdict/consecutively
Matt S
A: 
$srcdir = '/source/directory/';
$dh = opendir($srcdir);

$c = curl_init();
curl_setopt($c, ....); // set necesarry curl options to specify target url, etc...

while($file = readdir($dh)) {
    if (!is_file($srcdir . $file)) {
       continue; // skip non-files, like directories
    }
    curl_setopt($c, CURLOPT_POSTFIELDS, "file=@{$srcdir}{$file}");
    curl_exec($c);
}
closedir($dh);

That'd be the basics. You'd want some error handling in there, to make sure the source file is readable, to make sure the upload succeeds, etc.. The full set of CURLOPT constants are documented here.

Marc B
What about posting multiple files in one request? Can you post an array of files in one request?
Chris Muench
Marc B