PHP curl模拟form[type=file]上传文件

PHP curl模拟form[type=file]上传文件

作者:admin |  时间:2014-08-16 |  浏览:472 |  0 条评论
<?php
$contents =<<<‘TEXT’
上传文件在数据报中应该是
Content-Disposition: form-data; name="file1"; filename="3.txt"
Content-Type: 文档类型

文件内容

TEXT;

$varname = 'file1';
$name = '3.txt';
$type = 'text/plain';

$key = "$varname\"; filename=\"$name\r\nContent-Type: $type\r\nAccept: \"";
$fields[$key] = $contents;
$fields['dd'] = "pupu";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://10.16.15.225/qa/test1.php");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$s = curl_exec ($ch);

curl_close ($ch);

echo $s;
?>

服务端$_POST数组结果:
array(1) { ["dd"]=> string(4) "pupu" } 
$_FILES数组结果:
array(1) { ["file1"]=> array(5) { ["name"]=> string(5) "3.txt" ["type"]=> string(10) "text/plain" ["tmp_name"]=> string(14) "/tmp/phpI7dkkA" ["error"]=> int(0) ["size"]=> int(380) } } 

发表评论

电子邮件地址不会被公开。

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>