Не загружается картинка через AJAX
Появилась проблема - не загружается изображение на сервер. Я думаю, дело в AJAX, но так и не понял, что именно. Заранее спасибо.
HTML
<form id="imgur">
<input type="file" class="imgur" name="image">
</form>
AJAX
$('input[type=file]').on("change", function() {
var $files = $(this).get(0).files;
var formData = new FormData;
formData.append("image", $files[0]);
$.ajax({
async: false,
crossDomain: true,
processData: false,
contentType: false,
type: 'POST',
url: '',
data: {
test: formData
},
mimeType: 'multipart/form-data',
success: function(data) {
alert(data);
}
});
});
PHP
if (isset($_POST['test'])) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/upload.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer '.IMGUR_TOKEN));
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'image' => base64_encode(file_get_contents($_FILES['image']['tmp_name'])),
'album' => IMGUR_IMAGE_ALBUM,
'title' => 'title'
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$reply = json_decode($response);
$image = $reply->data->id;
echo $_FILES['image']['tmp_name'];
}