Helcio Posted October 7, 2009 Share Posted October 7, 2009 I am trying to upload images to Imageshack directly form my site with Imageshack API, using the code bellow: index.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form action="curl-form.php" method="post" enctype='multipart/form-data'> <input type="file" id="foto" name="foto" /> <input type="submit" value="Enviar" /> </form> </body> </html> img.php <?php $data = array(); $data['fileupload'] = '@' . $_FILES["foto"]['tmp_name']; $data['key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $data['email'] = 'email@email.com'; $data['rembar'] = 'yes'; $data['optsize'] = '96x96'; $data['xml'] = 'yes'; $post_str = ''; foreach($data as $key=>$val) { $post_str .= $key.'='.urlencode($val).'&'; } $post_str = substr($post_str, 0, -1); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.imageshack.us/upload_api.php'); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_TIMEOUT, 240); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($ch ); curl_close($ch ); if (strpos ( $response, '<' . '?xml version="1.0" encoding="iso-8859-1"?>' ) === false) { return NULL; } else { $xml = new SimpleXMLElement ( $response ); return array ( "image" => $xml->image_link, "thumb" => $xml->thumb_link ); } ?> When I upload an image, the following error occurs: "Sorry, but we've detected that unexpected data is received. Required parameter 'fileupload' is missing or your post is not multipart/form-data" What is wrong with the code? How can I send enctype='multipart/form-data' with cURL? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted October 7, 2009 Share Posted October 7, 2009 How can I send enctype='multipart/form-data' with cURL? add curl_setopt ( $ch , CURL_HTTPHEADER , "Content-type: multipart/form-data" ) EDIT: also please use code tags [ code] and welcome to PHPFreaks Quote Link to comment Share on other sites More sharing options...
Helcio Posted October 8, 2009 Author Share Posted October 8, 2009 I tried using different code: <?php $postData = array(); $postData['fileupload'] = "@" . $_FILES['foto']['tmp_name']; $postData['submit'] = "Submit"; $postData['key'] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $postData['email'] = "email@email.com"; $postData['rembar'] = "yes"; $postData['optsize'] = "96x96"; $postData['xml'] = "yes"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/upload_api.php"); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST, 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData ); $response = curl_exec( $ch ); curl_close($ch ); echo $response . "<br />"; ?> and the different error message was sent: Wrong file type detected for file php6JrCec:application/octet-stream cURL sent file using their temp name and the type verification of the Imageshack was refused. How can I send files to Imageshack using cURL? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted October 8, 2009 Share Posted October 8, 2009 please use code tags [ code] Wrong file type detected for file php6JrCec:application/octet-stream then set the Content-type to the type of the file! curl_setopt ( $ch , CURL_HTTPHEADER , "Content-type: ".$_FILES['foto']['type'] ) Quote Link to comment Share on other sites More sharing options...
Helcio Posted October 9, 2009 Author Share Posted October 9, 2009 I found a solution. I don´t know if this is the better solution, but works fine. index.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form action="imagem.php" method="post" enctype='multipart/form-data'> <input type="file" id="foto" name="foto" /> <input type="submit" value="Enviar" /> </form> </body> </html> imagem.php <?php preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $_FILES['foto']['name'], $ext); $imagem_nome = md5(uniqid(time())) . "." . $ext[1]; rename($_FILES['foto']['tmp_name'], "/tmp/" . $imagem_nome); $postData = array(); $postData['fileupload'] = "@/tmp/" . $imagem_nome; $postData['submit'] = "Submit"; $postData['key'] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $postData['rembar'] = "yes"; $postData['xml'] = "yes"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/index.php"); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_TIMEOUT, 240);curl_setopt($ch, CURLOPT_POSTFIELDS, $postData ); $response = curl_exec( $ch ); curl_close($ch ); echo $response; ?> I did not use the $_FILES['foto']['type'] function to get the file extension, because if the file type is jpg, this function returns pjpg, generating an error of invalid format in the Imageshack. The Imageshack then returns a xml that be parsed, with links to the image. I have another question. When I renamed the file in the temp directory, this file will be automatic deleted or I need to delete the file manually? Quote Link to comment Share on other sites More sharing options...
blance Posted March 24, 2011 Share Posted March 24, 2011 you also need to add the type of file it is in the fileupload field $postData['fileupload'] = "@/tmp/".$imagem_nome.";type=".$_FILES['data']['type']; as well as use the right api script curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/upload_api.php"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.