File: /var/www/html/s3-file-server/api-upload - Copy.php
<?php
if(isset($_POST['submit'])){
//Get the content of memCap.dat file which contains the maximus strage
//limit which is set by the admin
$capFile = fopen("src/set/memCap.dat", "r") or die("Unable to open file!");
$cap = fread($capFile,filesize("src/set/memCap.dat"));
fclose($capFile);
//Get the current folder size
function folderSize ($dir){
$size = 0;
foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) {
$size += is_file($each) ? filesize($each) : folderSize($each);
}
return $size;
}
$dirSize = (folderSize("uploads/")/1024)/1024/1024;
$precision = 2;
$dirSize = substr(number_format($dirSize, $precision+1, '.', ''), 0, -1);
//Get the total file size of files being uploaded (in GB)
$uploadSize = 0;
if(count($_FILES['upload']['name']) > 0){
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
$fSize = $_FILES['upload']['size'][$i]/1024/1024/1024;
$uploadSize = $uploadSize + $fSize;
}
}
else{
//Redirect to home page
// header('Location: index.php');
}
//Add current folder size and the size of the file being uploaded
//The size is in GB to match with storage cap set by admin
$totSize = $uploadSize + $dirSize;
//If the total size is less than the cap set
//Files can be uploaded
if($totSize<=$cap){
//Upload the files
//Get each files
$failUpload = 0;
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
$file_name = $_FILES['upload']['name'][$i];
$file_tmp = $_FILES['upload']['tmp_name'][$i];
$file_size = $_FILES['upload']['size'][$i];
$file_error = $_FILES['upload']['error'][$i];
$file_dir = "uploads/oth/";
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
$imageName = generateRandomString();
$newFileName = $imageName . '-' . time() . '.' . $extension;
//Prevent upload of certain files that can cause security issues
if($file_ext=="php" || $file_ext=="html"){
$failUpload++;
continue;
}
else{
$file_error = 0;
if($file_error === 0) {
if($file_ext == "png" || $file_ext == "jpg" ||$file_ext == "jpeg" ||$file_ext == "gif" ||$file_ext == "bmp"){
$file_dir = "uploads/img/";
}
elseif ($file_ext == "mp4" ||$file_ext == "avi" ||$file_ext == "mkv" ||$file_ext == "mpeg" ||$file_ext == "mov") {
$file_dir = "uploads/vid/";
}
elseif ($file_ext == "mp3" ||$file_ext == "flac") {
$file_dir = "uploads/aud/";
}
$file_destination = $file_dir . $newFileName;
$url=$_POST['img_url'];
$data = file_get_contents($url);
if(file_put_contents($file_destination, $data))
{
echo url().$file_destination;
}
if(move_uploaded_file($file_tmp, $file_destination)) {
continue;
}
else
$failUpload++;
}
}
}
//Redirect to home page
if($failUpload>0)
{
// header('Location: index.php?e=1');
}
else
{
// header('Location: index.php');
//header('Location: index.php');
}
}
//Storage cap exceeded. Error message is shown
else{
echo "Storage limit exceeded! Delete some files or increase the storage limit from dashboard.";
}
}
function generateRandomString($length = 30) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
function url(){
if(isset($_SERVER['HTTPS'])){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
}
else{
$protocol = 'http';
}
$currentDir = basename(__DIR__);
return $protocol . "://" . $_SERVER['HTTP_HOST']."/".$currentDir."/";
}
?>