Upload a file and submit a form with curl
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
Maybe that be helpful http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
精彩评论