使用curl上传文件并提交表单
我正在尝试提交表单,其中一个表单值是文件上传。 cookie 一切正常,我只是将代码的前一部分遗漏了。我收到回复说上传失败,表单数据丢失。这是表单页面的源代码 http://pastebin.com/zdyzcRrn 我做错/遗漏了什么出去?
<?php
//Cookie file location
$cookie_file_path = "c:/cookie.txt";
//Upload Url - the page that the form goes to. The actual form is on upload.php
$UploadUrl = "http://www.farm.co.za/takeupload.php";
//create array of data to be posted
$post_data['file'] = "@/MythBusters.S09E14.Newtons.Crane.Cradle.HDTV.XviD-FQM.torrent";
$post_data['name'] = 'MythBusters.S09E14';
$post_data['type'] = '7';
$post_data['nfo'] = '';
$post_data['descr'] = 'MythBusters.S09E14';
$post_data['submit'] = 'Do it!';
//traverse array and prepare data for posting
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$UploadPostInfo = implode ('&', $post_items);
//create cURL connection
$cr = curl_init();
curl_setopt($cr, CURLOPT_HEADER, false);
curl_setopt($cr, CURLOPT_NOBODY, false);
curl_setopt($cr, CURLOPT_URL, $UploadUrl);
curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cr, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($cr, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($cr, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cr, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cr, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($cr, CURLOPT_POST, 1);
curl_setopt($cr, CURLOPT_POSTFIELDS, $UploadPostInfo);
$html=curl_exec($cr);
print $html;
curl_close($cr);
?>
感谢您的帮助
I am trying to submit a form and one of the form values is a file upload. The cookies all work I just left the previous part of the code out. I am getting a response saying Upload failed form data missing. This is the source code of the form page http://pastebin.com/zdyzcRrn what am I doing wrong/missing out?
<?php
//Cookie file location
$cookie_file_path = "c:/cookie.txt";
//Upload Url - the page that the form goes to. The actual form is on upload.php
$UploadUrl = "http://www.farm.co.za/takeupload.php";
//create array of data to be posted
$post_data['file'] = "@/MythBusters.S09E14.Newtons.Crane.Cradle.HDTV.XviD-FQM.torrent";
$post_data['name'] = 'MythBusters.S09E14';
$post_data['type'] = '7';
$post_data['nfo'] = '';
$post_data['descr'] = 'MythBusters.S09E14';
$post_data['submit'] = 'Do it!';
//traverse array and prepare data for posting
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$UploadPostInfo = implode ('&', $post_items);
//create cURL connection
$cr = curl_init();
curl_setopt($cr, CURLOPT_HEADER, false);
curl_setopt($cr, CURLOPT_NOBODY, false);
curl_setopt($cr, CURLOPT_URL, $UploadUrl);
curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cr, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($cr, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($cr, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cr, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cr, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($cr, CURLOPT_POST, 1);
curl_setopt($cr, CURLOPT_POSTFIELDS, $UploadPostInfo);
$html=curl_exec($cr);
print $html;
curl_close($cr);
?>
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这会有所帮助 http://dtbaker .com.au/random-bits/uploading-a-file-using-curl-in-php.html
Maybe that be helpful http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html