-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploads.php
70 lines (61 loc) · 2.36 KB
/
uploads.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
* Its an upload script which stores users file
*
* @author Ashish Maurya
* @company Initedit
* @companyurl http://initedit.com
* @since 07-Apr-2018
* @version 0.0.1
*/
include "config.php";
$UPLOAD_DIR = $config["upload_dir"];
// Make Upload Directary if it does not exists already
if(!file_exists($UPLOAD_DIR)){
mkdir($UPLOAD_DIR,"775");
}
// Check if user uploaded or not
$USER_UPLOADED_FILE = isset($_FILES["file"])?$_FILES["file"]:null;
$result = ["code"=>100,"message"=>"Something broken..."];
if($USER_UPLOADED_FILE!=null){
$USER_UPLOADED_FILE_NAME = $USER_UPLOADED_FILE["name"];
$USER_UPLOADED_FILE_TMP_NAME = $USER_UPLOADED_FILE["tmp_name"];
$USER_UPLOADED_FILE_NAME_SAVE = $USER_UPLOADED_FILE_NAME;
$FILE_NAME_PREFIX = null;
$PREFIX_SEPERATER = $config["prefix_seperator"];
while(file_exists($UPLOAD_DIR.$FILE_NAME_PREFIX.$PREFIX_SEPERATER.$USER_UPLOADED_FILE_NAME)){
$FILE_NAME_PREFIX .= time().rand(0,100);
}
if($FILE_NAME_PREFIX!=null){
$USER_UPLOADED_FILE_NAME_SAVE = $FILE_NAME_PREFIX.$PREFIX_SEPERATER.$USER_UPLOADED_FILE_NAME;
}else{
$USER_UPLOADED_FILE_NAME_SAVE = $PREFIX_SEPERATER.$USER_UPLOADED_FILE_NAME;
}
$is_saved = move_uploaded_file($USER_UPLOADED_FILE_TMP_NAME,$UPLOAD_DIR.$USER_UPLOADED_FILE_NAME_SAVE);
if($is_saved){
$result["message"] = "uploaded";
$result["code"] = 1;
if($config["upload_clamp"]){
$recent_files = [];
$all_file_list = glob($config["upload_dir"]."*", GLOB_BRACE);
usort($all_file_list,function($a,$b){
return filemtime($a) < filemtime($b);
});
foreach ($all_file_list as $filename) {
$recent_files[] = basename($filename);
}
if($config["recent_count"]<count($recent_files)-2){
$remove_file_name = end($recent_files);
unlink($UPLOAD_DIR.$remove_file_name);
$result["dev"]="Removed Last Element as Clamp was enabled";
$result["devfile"]=$remove_file_name;
}
}
}else{
$result["message"] = "Unable to upload";
$result["dev"] ="move_uploaded_file operation failed. Could be cause of file permission or disk size";
}
}else{
$result["message"] = "No File Found";
}
echo json_encode($result);