使用PHP和卷曲后的HTML表单,包括文件
问题描述:
我有一个这样的形式:使用PHP和卷曲后的HTML表单,包括文件
<form method="POST" action="i.php" enctype="multipart/form-data">
<input type="text" name="field1">
<input type="text" name="field2">
<input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="submit">
</form>
在网页上我已经有了:
$URL = 'http://somewhere.com/catch.php';
$fields = array('field1'=>urlencode($_POST['field1'), 'field2'=>urlencode($_POST['field2'));
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; };
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($ch);
curl_close($ch);
,致力于发布FIELD1 & 2个字段。 是否有可能在某个卷曲过程中包含该文件?我会怎么做?我假设我必须做的不仅仅是对文件值进行编码。
因此,基于SimpleCoders的回答我更新以下内容:
$URL = 'http://somewhere.com/catch.php';
$fields = array('field1'=>urlencode($_POST['field1'), 'field2'=>urlencode($_POST['field2'), 'files'=>'@'. $_FILES['file']['tmp_name']);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; };
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($ch);
curl_close($ch);
哪些职位确定,但随后在catch.php我产生$ _FILES数组为空。在http://www.php.net/manual/en/function.curl-setopt.php上注册到示例#2这应该起作用。
我正在做这两个不同的领域......可能是一个问题?
答
尝试这样的事情,采取from here:
$file_to_upload = array('file_contents'=>'@'.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
还要考虑https://secure.php.net/manual/en/class.curlfile.php,这似乎是这样做的更简单,更现代的方式。
答
您需要按照SimpleCoders答案,但之后又改变了
$fields_string
只是
$fields.
从http://www.php.net/manual/en/function.curl-setopt.php
注:将数组传递给CURLOPT_POSTFIELDS将编码数据作为多/ form-data,同时传递一个URL编码的字符串会将数据编码为application/x-www-form-urlencoded。
由于无法保证外部链接的生命,您是否可以在此处发布信息并保留参考? – 2016-04-20 18:56:34
@感谢:确定的事情,完成 – 2016-04-20 20:46:47
社区和那些来,谢谢。 :) – 2016-04-21 00:34:24