Preserve name of file using cURL to transfer files
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 (compa开发者_开发知识库tible; 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
You can rename the file to it's original name using move_uploded_file().
move_uploded_file($_FILES['thefile']['tmp_name'][0], $your_uploads_dir.'/'.$_FILES['thefile']['name'][0]);
$postargs = array(
'nonfilefield' =>'nonfilevalue',
'fileentry' => '@'.$your_uploads_dir.'/'.$_FILES['thefile']['name'][0]);
Nevermind, this will suffice...
$postargs = array(
'nonfilefield'=>'nonfilevalue',
$_FILES['thefile']['name'][0] => '@'.$_FILES['thefile']['tmp_name'][0]
);
精彩评论