Hello, I have a problem uploading an image through api. Here's how I'm trying to use it:
Upload form with uploadfive.js http://img.smilley.ru/index2.php:
JS code for that form:
test.php:
But it fails to upload anything. Any ideas please?
Best regards,
Vladislav.
Upload form with uploadfive.js http://img.smilley.ru/index2.php:
HTML:
<form role="form">
<div class="form-group">
<label for="exampleInputFile">Выберите файлы</label>
<input type="file" multiple="true" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Code:
$(function() {
$('#exampleInputFile').uploadifive({
'uploadScript' : 'test.php',
'onUploadFile' : function(file) {
alert(file.name + 'is being uploaded.');
},
'onUploadComplete' : function(file, data) {
alert('The file ' + file.name + ' uploaded successfully. Data:'+data);
},
'removeCompleted' : true
});
});
test.php:
PHP:
<?php
error_reporting(E_ALL);
$url = '[url]http://img.smilley.ru/api[/url]';
// What do we send to chevereto api?
$fields = array(
// The key, in this case my_api_key (the default one)
'key' => urlencode('api'),
// The image encoded in base64
'upload' => base64_encode(file_get_contents($_FILES['Filedata']['tmp_name'])),
// And how the api answer us?
'format' => urlencode('txt')
);
//open connection
$ch = curl_init();
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, sizeof($fields));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);
#echo file_get_contents($_FILES['Filedata']['tmp_name']);
?>
But it fails to upload anything. Any ideas please?
Best regards,
Vladislav.