<?php
// the below json header doesn't work, I don't know why
// header("Content-type: application/json");
// the return type must be text/html, if not a dialog to download the php backend script (yui_image_uploader.php) appears
header("Content-type: text/html");
try {
// if the file has been sent successfully and that file is a image, then store the image in the server
if ($_FILES['picture']['error'] == UPLOAD_ERR_OK) {
$file_type = basename($_FILES['picture']['type']);
switch ($file_type) {
case 'jpeg':
case 'pjpeg':
$filename = 'yui.jpg';
break;
default:
throw new Exception('The uploaded file is not a JPEG image');
break;
}
// set the directory in the server where I want to store the uploaded image
$images_directory = 'images/';
$uploadpath = $images_directory . $filename;
// upload the image
if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadpath)) {
echo json_encode(array('status'=>'UPLOADED', 'image_url'=>$uploadpath));
} else {
throw new Exception('Possible file upload attack');
}
} else {
throw new Exception('Problem trying to upload the file');
}
} catch (Exception $e) {
$status = $e->getMessage();
echo json_encode(array('status'=>$status));
}
?>