Last active
June 9, 2017 16:55
-
-
Save Venipa/bc941e20d9712cd0496d60eeb0a6aab2 to your computer and use it in GitHub Desktop.
Simple File Upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$target_dir = "uploads/".date("Y-m-d"); | |
if(!file_exists($target_dir)) { | |
mkdir($target_dir); | |
} | |
$maxfilesize = 200; //MB | |
$filetype = ".".pathinfo($_FILES["files"]["name"], PATHINFO_EXTENSION); | |
$target_file = $target_dir . "/[" . date("h.m.s") . "]" . random_str(2) . "_" . $_FILES["files"]["name"]; | |
$key = "Qvnpk7wVaYqHDEqkSCw31YWUd7bmNVaJbwsokX9aqzGDlhMb4a"; | |
$postkey = isset($_POST["key"]) ? stripslashes(trim($_POST["key"])) : null; | |
$getkey = isset($_GET["key"]) ? stripslashes(trim($_GET["key"])) : null; | |
header('Content-Type: application/json'); | |
if($getkey === $key) { | |
echo json_encode([ | |
'Name' => $_SERVER[HTTP_HOST], | |
'DestinationType' => 'None', | |
'RequestUrl' => $_SERVER[HTTP_HOST]."/".$_SERVER['PHP_SELF'], | |
'RequestType' => 'POST', | |
'FileFormName' => 'files', | |
'Arguments' => [ | |
'key' => $key | |
], | |
'ResponseType' => 'Json', | |
'URL' => '$JSON:[\'url\']$' | |
], JSON_PRETTY_PRINT); | |
die(); | |
} | |
//Check Filesize | |
if(filesize($_FILES["files"]["temp_name"]) >= (pow(1024,2) * $maxfilesize)) { | |
echo json_encode(["error" => "File is to big."], JSON_PRETTY_PRINT); | |
http_response_code(403); | |
die(); | |
} | |
if($target_file == null) { | |
echo json_encode(["error" => "No File has been detected."], JSON_PRETTY_PRINT); | |
http_response_code(403); | |
die(); | |
} | |
if($postkey != null && $postkey === $key) { | |
if(file_exists($target_file)) { | |
echo json_encode(['url' => "http://".$_SERVER[HTTP_HOST]."/".$target_file], JSON_PRETTY_PRINT); | |
} else { | |
move_uploaded_file($_FILES["files"]["tmp_name"], $target_file); | |
echo json_encode(['url' => "http://".$_SERVER[HTTP_HOST]."/".$target_file], JSON_PRETTY_PRINT); | |
} | |
} else { | |
echo json_encode(["error" => "Wrong Key."], JSON_PRETTY_PRINT); | |
http_response_code(405); | |
die(); | |
} | |
function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') | |
{ | |
$str = ''; | |
$max = mb_strlen($keyspace, '8bit') - 1; | |
for ($i = 0; $i < $length; ++$i) { | |
$str .= $keyspace[random_int(0, $max)]; | |
} | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment