<?php
// ini_set('display_errors', '1');
// ini_set('display_startup_errors', '1');
// error_reporting(E_ALL);
include('db.php');
$referral_id = isset($_REQUEST['referral_id']) ? base64_decode($_REQUEST['referral_id']) : '';
if(!empty($referral_id) && !empty($_FILES)){
        $filename = trim(preg_replace("/-+/", "-", preg_replace("/[^a-z0-9]+/", "-", strtolower(pathinfo($_FILES["upload"]["name"], PATHINFO_FILENAME)))), "-") . "-" . substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8) . "." . strtolower(pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION));
    move_uploaded_file($_FILES['upload']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/code/uploads/'. $filename);
    $update_suceess = $db->query("UPDATE wp_referral_form SET `attachment` = '/code/uploads/".$filename."' WHERE id = ".$referral_id);
    if($update_suceess){
        $response['status'] = 1;
        $response['referral_id'] = base64_encode($referral_id);
    }else{
        $response['status'] = 0;
        $response['referral_id'] = '';
    }
} else {
    $response['status'] = 0;
    $response['referral_id'] = '';
}
echo json_encode($response);
die;