I'm trying to recreate an entire http request includ开发者_StackOverflowing both post and files data however no matter what I do I can't seem to get my files to work, the code I'm using is below...
$count=count($_FILES['photographs']['tmp_name']);
$file_posts=array();
for($i=0;$i<$count;$i++) {
if(!empty($_FILES['photographs']['name'][$i])) {
$_FILES['photographs']['tmp_name'][$i] = "@".$_FILES['photographs']['tmp_name'][$i];
}
}
$post = array_merge($_POST, $_FILES);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://url/to/file.php");
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch,CURLOPT_HEADER,TRUE);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_exec($ch);
curl_close($ch);
I've tried many variants of this but I can't seem to get files to work no matter what, other post data is fine however.
The problem is that there is a second 'layer' of arrays in your $post
.
You need to do:
$file_posts=array();
for($i=0;$i<$count;$i++) {
if(!empty($_FILES['photographs']['name'][$i])) {
$file_posts['photographs'][$i] = "@".$_FILES['photographs']['tmp_name'][$i];
}
}
精彩评论