Preserve name of file using cURL to transfer files

Posted by Toby on Stack Overflow See other posts from Stack Overflow or by Toby
Published on 2010-06-08T12:41:58Z Indexed on 2010/06/08 12:42 UTC
Read the original article Hit count: 168

Filed under:
|
|
|

I'm transferring files from an existing http request using cURL like so...

$postargs = array(
'nonfilefield' =>'nonfilevalue',           
'fileentry' => '@'.$_FILES['thefile']['tmp_name'][0]

);

$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, true); curl_setopt($ch,CURLOPT_POST,TRUE); curl_setopt($ch,CURLOPT_POSTFIELDS,$postargs); curl_exec($ch); curl_close($ch);

The only way I can get this to work is using the tmp_name, without this it won't send. However, I then lose the name value for when I want to name the file later.

Is there some way to do this preserving the $_FILES array as it normally would be without curl? I'm also using an array of file fields in my script, so at the moment I have to convert my multidimensional array into a single dimension for this to work

© Stack Overflow or respective owner

Related posts about php

Related posts about http