upload image error with php class and curl -
hi have code uploader.php:
class uploader { var $filepath; var $uploadurl; var $formfilevariablename; var $postparams = array (); function uploader($filepath, $uploadurl, $formfilevariablename, $otherparams = false) { $this->filepath = $filepath; $this->uploadurl = $uploadurl; if(is_array($otherparams) && $otherparams != false) { foreach($otherparams $fieldname => $fieldvalue) { $this->postparams[$fieldname] = $fieldvalue; } } $this->postparams[$formfilevariablename] = "@" . $filepath; } function uploadfile() { $ch = curl_init(); curl_setopt($ch, curlopt_url, $this->uploadurl); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $this->postparams); curl_setopt($ch, curlopt_returntransfer, 1); $postresult = curl_exec($ch); if (curl_errno($ch)) { print curl_error($ch); print "unable upload file."; exit(); } curl_close($ch); return $postresult; } }
and use this:
require('uploader.php'); $upload_server = "http://developers.mytech.com.mx/files/api/upload.php"; $file = $_files['archivo']['tmp_name']; $archivo = $file; $extension = pathinfo($archivo, pathinfo_extension); $nombre_base = basename($archivo, '.'.$extension); $upload = new uploader($file, $upload_server,'file', array('api_key' => 'ic00n6yokd')); $result = $upload->uploadfile(); if(preg_match("/upload_failed/", $result)){ echo "upload failed."; } if(preg_match("/error_tamano/", $result)){ echo "archivo muy grande."; } if(preg_match("/api_invalida/", $result)){ echo "invalid api code."; } if(preg_match("/error_type/", $result)){ echo "archivo no valido."; } if(preg_match("/upload_success/", $result)){ echo $result; }
but problem wht uploads tmp_name file , not image if change line: $file = $_files['archivo']['tmp_name']; $file = $_files['archivo']['name'];
i error: failed creating formpost data
my question how use class orectly can upload image
maybe should specify headers image data try this:
curl_setopt($ch, curlopt_httpheader, array( 'content-type: image/png' ));
Comments
Post a Comment