first commit
This commit is contained in:
52
upload.php
Normal file
52
upload.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', TRUE);
|
||||
ini_set('display_startup_errors', TRUE);
|
||||
|
||||
$root = $_SERVER["DOCUMENT_ROOT"];
|
||||
|
||||
$target_dir = "$root/uploads/";
|
||||
|
||||
if(! isset($_FILES["file"])) {
|
||||
header("HTTP/1.1 400 No file Upload");
|
||||
//trigger_error("No file Upload",E_USER_ERROR);
|
||||
exit();
|
||||
}
|
||||
|
||||
$file = $_FILES["file"];
|
||||
|
||||
$name = basename($file["name"]);
|
||||
|
||||
if(! mb_check_encoding($name)) {
|
||||
header("HTTP/1.1 400 Bad encode");
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
$size = basename($file["size"]);
|
||||
|
||||
$target_file = $target_dir.$name;
|
||||
|
||||
// Check if file already exists
|
||||
if (file_exists($target_file)) {
|
||||
header("HTTP/1.1 400 This file already exists");
|
||||
echo "This file already exists.";
|
||||
}
|
||||
else {
|
||||
if (move_uploaded_file($file["tmp_name"], $target_file)) {
|
||||
echo "The File ".$name." of size ".formatBytes($size)." has been uploaded.";
|
||||
} else {
|
||||
header("HTTP/1.1 500");
|
||||
echo "There was an error uploading your file.";
|
||||
}
|
||||
}
|
||||
|
||||
function formatBytes($size, $precision = 2)
|
||||
{
|
||||
$base = log($size, 1024);
|
||||
$suffixes = array('', 'KiB', 'MiB', 'GiB', 'TiB');
|
||||
|
||||
return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user