PHP Forums Archive

cURL File upload array

Tags: upload, curl

smallfish posted on 2009-12-21 08:58:55 #

I'm trying to upload files via cURL and it works fine except when the file field is an array - in that case it just sends 'array' and it ends up as a regular post field.

$ch = curl_init('http://example.com/files_test.php');

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);

if (count($_POST) > 0 || count($_FILES) > 0) {
	foreach($_POST as $key=>$val) {
		$params[$key]=$val;
	}
	foreach($_FILES as $key=>$val) {
		if(is_array($val['tmp_name'])) {
			foreach($val['tmp_name'] as $id=>$filename) {
				if($val['error'][$id]==0) {
					$params[$key][$id]='@'.$filename;
				}
			}
		} else {
			$params[$key]='@'.$val['tmp_name'];
		}
	}
	curl_setopt($ch, CURLOPT_POST,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
}

$data = curl_exec($ch);
$content_type = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );
curl_close($ch);

before the curl call the content of $params is correct - eg.

Array
	(
	[upload_file1] => @/tmp/phpQVxDa0
	[upload_file2] => @/tmp/php5KiAPY
	[upload_file3] =>
	Array
		(
		[0] => @/tmp/phppf1Z24
		[1] => @/tmp/phptbYhhl
		)
	)

but after the curl call $parms has been altered to:

Array
	(
	[upload_file1] => @/tmp/phpQVxDa0
	[upload_file2] => @/tmp/php5KiAPY
	[upload_file3] => Array
	)

Is there any way around this? Also - will the same happen to regular post fields?

Comments / discussions

Olaf posted on 2009-12-21 09:57:58 #

Hi,

I remember me some limit while sending multiple files via cURL.
do you trued to use the multi curl functions?

how about using the whole curl function inside a loop?