Upload multiple files with curl

software development

#1

I’m building an app to automatically update a bunch of template sites. I’ve got the file upload to the remote site working for one file, now I need to know how to upload several at one time.

Can I do this with one curl call? Or do I need to upload each file individually? If I set the infile to an array and calculate the filesize for all the files, will that work?

See code below:

$u_name = $row[“ftp_user”];
$p_word = $row[“ftp_password”];
$up_file = ‘test.php’;
$srvr_path = ‘/’.$row[“domain_url”].’/’;
$new_name = ‘test.php’;

echo $server;
echo $u_name;
echo $p_word;
// Append path & uploaded file name to server name, pass with CURLOPT_URL.

$url = $server . $srvr_path . $new_name;

// fopen() upload file for handle to pass with CURLOPT_INFILE.
$fp = @fopen($up_file, ‘r’);
if (!$fp) {
echo ‘Failed to open upload file.’ . ‘
’;
exit;
}
if (!($ch = curl_init())) {
echo ‘Unable to allocate resource.’ . ‘
’;
exit;
}

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $u_name . ‘:’ . $p_word);
// If true w/CURLOPT_RETURNTRANSFER server output returned by curl_exec().
// (Usually no output w/FTP, anyway.)
// If false, curl_exec() will return true on success.
// Either way curl_exec() returns false on failure.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_INFILE, $fp);
// Pass true w/CURLOPT_TRANSFERTEXT for ascii, false for binary.
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
// File size w/CURLOPT_INFILESIZE must be accurate for clean transfer.
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($up_file));
// Everything’s ready, now curl_exec() does it all.

$curl_return = curl_exec($ch);

if ($curl_return === false) {
// Show failure info.
echo ‘curl_exec() failed.’ . ‘
’;
echo 'curl_errno() = ’ . curl_errno($ch) . ‘
’;
echo 'curl_error() = ’ . curl_error($ch) . ‘
’;
} else {
// Show success info.
echo ‘curl_exec() succeeded.’ . ‘
’;
echo ‘
curl_getinfo()’ . ‘
’;
foreach (curl_getinfo($ch) as $key => $value) {
echo $tab . $key . ’ => ’ . $value . ‘
’;
}
}
curl_close($ch);
}

================================
Angela Gann
CrimsonDryad Web Design Services
Web Design, Custom Software Development
http://www.crimsondryad.com


#2

Well, I decided to loop through the files and make individual calls. I dunno if this is the most efficient way, but it works.

================================
Angela Gann
CrimsonDryad Web Design Services
Web Design, Custom Software Development
http://www.crimsondryad.com