<?php
$target_dir = "public/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$target_filenameonly = basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
        header('Location: ../imeji/uploadfailed.php');
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
    header('Location: ../imeji/uploadfailed.php');
}
// Check file size (Currently Max 6MB)
if ($_FILES["fileToUpload"]["size"] > 6291456) {
    echo "Sorry, your file is too large. (Max is 6MB)";
    $uploadOk = 0;
    header('Location: ../imeji/uploadfailed.php');
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
    header('Location: ../imeji/uploadfailed.php');
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file could not be uploaded.";
    header('Location: ../imeji/uploadfailed.php');
// if everything is ok, try to upload file
} else {
    //$newfilename = time() . '_' . rand(1000000, 9999999) . '.' . end(explode(".",$_FILES["fileToUpload"]["name"]));
    $newfilename = time() . '_' . rand(100, 999) . '_' . rand(100000000, 999999999) . '.' . end(explode(".",$_FILES["fileToUpload"]["name"]));
    $newtarget = $target_dir . $newfilename;
    //if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newtarget)) {
        chmod("$newtarget", 0775); // Set read and write permissions if file
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        header('Location: http://i.zxicar.us/' . $newfilename);
        //header('Location: ../imeji/' . $newtarget); --changed as of v0.7
        //header('Location: http://zxicar.us/imeji/'.basename( $_FILES["fileToUpload"]["name"])); --changed as of v0.2
    } else {
        echo "Sorry, there was a problem uploading your file.";
    }
}
?>