//upload_file.php <?php /** * class Manager */ class Manager { // the message public $sendResponse; function __construct() { $sendResponse = ""; } /** * Returns data in JSON format * @param $success upload true or false * @param $message response the message * @param $code default 200 */ private function message( $success, $message = NULL, $code = 200 ) { $response = array( 'success' => (bool) $success, 'message' => $message, 'code' => (int) $code ); header('Content-Type: application/json;charset=utf-8', TRUE, $httpCode); $this->sendResponse = json_encode($response); } /** * A function to upload files * @param $file Uploaded file from front-end */ public function upload_file($file, $folder='upload') { if (isset($file)) { if ($file["error"] > 0){ $this->message(FALSE, "Find a incorrect"); } else{ if (file_exists($folder . "/" . $file["name"])){ $msg = $file["name"] . " already exists. "; $this->message(FALSE, $msg); }else { move_uploaded_file($file["tmp_name"], $folder . "/" . $file["name"]); $this->message(TRUE, "Success"); } } } else{ echo "Invalid file"; } } } $file = $_FILES["file"]; $manager = new Manager(); $manager->upload_file($file); echo $manager->sendResponse; ?>