Hola bueno gracias de antemano por leer este post
mi problema es el siguiente:
"Estoy haciendo una API para un sitio web de imagenes y debo subir la imagen usando Curl a el servidor y hasta ahora lo he logrado todo, el archivo se sube, pero tengo un problema y es que me sube es el .tmp y no el jpg o png que desee subir y cuando hago print_r da como resultado esto"
Array ( [media] => Array ( [name] => phpo7p3p4 [type] => application/octet-stream [tmp_name] => /tmp/phpyqVTI1 [error] => 0 [size] => 119258 ) )
la funcion que estoy usando para subir es esta.
Código PHP:
<?php
class twitpic
{
/*
* variable declarations
*/
var $post_url='http://localhost/floggiaAPI/gateway.php';
var $post_tweet_url='http://localhost/floggiaAPI/gateway.php';
var $url='';
var $post_data='';
var $result='';
var $tweet='';
var $return='';
/*
* @param1 is the array of data which is to be uploaded
* @param2 if passed true will display result in the XML format, default is false
* @param3 if passed true will update status twitter,default is false
*/
function __construct($data,$return=false,$tweet=false)
{
$this->post_data=$data;
if(empty($this->post_data) || !is_array($this->post_data)) //validates the data
$this->throw_error(0);
$this->display=$return;
$this->tweet=$tweet;
}
function post()
{
$this->url=($this->tweet)?$this->post_tweet_url:$this->post_url; //assigns URL for curl request based on the nature of request by user
$this->makeCurl();
}
private function makeCurl()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_POST, 3);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->post_data);
$this->result = curl_exec($curl);
curl_close($curl);
if($this->display)
{
echo $this->result ;
}
}
private function throw_error($code) //handles few errors, you can add more
{
switch($code)
{
case 0:
echo 'Think, you forgot to pass the data';
break;
default:
echo 'Something just broke !!';
break;
}
exit;
}
} //class ends here
?>
Código PHP:
<?php
if ($_POST)
{
include 'client-front-end/twitpic.php';
$file = $_FILES['media'];
$postfields = array();
$postfields['photo_title'] = $_POST['photo_title'];
$postfields['photo_user'] = $_POST['photo_user'];
$postfields['photo_description'] = $_POST['photo_description'];
$postfields['media'] = "@$file[tmp_name]";
$t = new twitpic($postfields, true, true);
$t->post();
exit;
}
?>
<style type="text/javascript">
*{font-family:verdana;}
span{font-size:12px;color:#393939;}
h3{font-size:14px;color:#5AAAF7;}
</style>
<body>
<h3>Floggia Demo Upload Picture</h3>
<form method="post" enctype="multipart/form-data" action="twitpic.php" >
<p><span style="height:40px;font-weight:bold;margin-right:56px;">Photo Title :</span>
<input type="text" name="photo_title" id="photo_title" /></p>
<p><span style="height:40px;font-weight:bold;margin-right:61px;">User ID:</span><input type="password" name="photo_user" id="photo_user" /></p>
<p><span style="vertical-align:text-top;height:40px;font-weight:bold;margin-right:28px;">Description :</span>
<textarea cols="35" rows="2" name="photo_description" id="photo_description"></textarea></p>
<p><span style="vertical-align:text-top;height:40px;font-weight:bold;">Choose an image to upload: </span><input type="file" name="media" /></p>
<p style="width:250px;text-align:right;margin-top:50px;"><input type="submit" value="Upload »" /> </p>
</form>
</body>