Using PHP and Curl to POST an entire folder to another Server
What is the best way using PHP 开发者_JAVA技巧and Curl to POST an entire folder to another server.
you can:
post all files in the directory consequently
zip the directory and post the archive
$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.
精彩评论