note 87277 added to function.curl-setopt

From: Date: Thu, 27 Nov 2008 13:46:33 +0000
Subject: note 87277 added to function.curl-setopt
Groups: php.notes 
Request: Send a blank email to php-notes+get-147035@lists.php.net to get a copy of this message
I've a problem when tried to use upload file with different content-type (adding ;type=content-type after filename). I write below a workaround to fix this, and now works for me. It's substitue curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arrData)); If you have the same problem, use this solution. PS: If you have simulate upload with no file, use "filename" => "@" only. <?php function postWithFiles($ch, $arrData) { $arrHeaders = array(); $strContents = ""; // Generate unique ID $strUniqueId = substr(md5(uniqid(rand(), true)), 0, 12); $strBoundary = "----------------------------". $strUniqueId; // Overwrite Content-Type header for multipart/form-data - First Boundary $arrHeaders[] = "Content-Type: multipart/form-data; boundary=". $strBoundary; curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeaders); foreach ($arrData as $strName => $strData) { // Add Boundary $strContents .= "--". $strBoundary ."\r\n"; // Check if is file and if contents content type tag if (preg_match("/@([^;]*)(;type=(.*))?/", $strData, $arrFile)) { $strFileName = $arrFile[1]; $strContentType = (!empty($arrFile[3])) ? $arrFile[3] : "application/octet-stream"; $strContents .= 'Content-Disposition: form-data; name="'. $strName .'"; filename="'. basename($strFileName) .'"'."\r\n"; $strContents .= 'Content-Type: '. $strContentType ."\r\n"; $strContents .= "\r\n"; if (file_exists($strFileName)) $strContents .= file_get_contents($strFileName); $strContents .= "\r\n"; } else { $strContents .= 'Content-Disposition: form-data; name="'. $strName .'"'."\r\n"; $strContents .= "\r\n"; $strContents .= $strData ."\r\n"; } } // Last Boundary $strContents .= "--". $strBoundary ."--\r\n"; curl_setopt($ch, CURLOPT_POSTFIELDS, $strContents); } ?> ---- Server IP: 200.185.126.110 Probable Submitter: 189.70.180.78 ---- Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php Edit -- https://master.php.net/note/edit/87277 Del: integrated -- https://master.php.net/note/delete/87277/integrated Del: useless -- https://master.php.net/note/delete/87277/useless Del: bad code -- https://master.php.net/note/delete/87277/bad+code Del: spam -- https://master.php.net/note/delete/87277/spam Del: non-english -- https://master.php.net/note/delete/87277/non-english Del: in docs -- https://master.php.net/note/delete/87277/in+docs Del: other reasons-- https://master.php.net/note/delete/87277 Reject -- https://master.php.net/note/reject/87277 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#147035) next »