3

I'm having some trouble sending $_FILES by cURL - the files get transferred alright using the following code, however it is impossible for me to get the name, and type of the file, in fact once the $_FILES reach their destination their type is stored as "application/octet-stream" - what am I doing wrong!?

 $count=count($_FILES['thefile']['tmp_name']);

 for($i=0;$i<$count;$i++) {
  if(!empty($_FILES['thefile']['name'][$i])) {
   $postargs[$i] = '@'.$_FILES['thefile']['tmp_name'][$i];
  }
 }


 $header = array("Content-type: multipart/form-data");
 $ch = curl_init('http://localhost/curl/rec.php');
 curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");  
 curl_setopt($ch,CURLOPT_RETURNTRANSFER, false); 
 curl_setopt($ch,CURLOPT_ENCODING,"");
 curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
 curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
 curl_setopt($ch,CURLOPT_POST,TRUE);
 curl_setopt($ch,CURLOPT_POSTFIELDS,$postargs);
 curl_exec($ch);
 curl_close($ch)

1 Answer 1

6

Your should construct $postargs like this,

 $postargs = array();
 foreach ($_FILES as $param => $file) {
     $postargs[$param] = '@' . $file['tmp_name'] . ';filename=' . $file['name'] . ';type=' . $file['type'];
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.