PHP Classes

cURL File upload array

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  cURL File upload array  
Subject:cURL File upload array
Summary:cURL wont upload files as array
Messages:2
Author:Bob
Date:2009-10-06 10:02:26
Update:2009-10-14 03:58:19
 

  1. cURL File upload array   Reply   Report abuse  
Picture of Bob Bob - 2009-10-06 10:19:19
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?

There is 1 reply in this thread, which is not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.