-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalib.php
More file actions
69 lines (58 loc) · 1.91 KB
/
alib.php
File metadata and controls
69 lines (58 loc) · 1.91 KB
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
<?php
//
// ALIB - ALib Isnt Bad
// Multipurpose PHP Library
// ALIB is licensed under MIT
// https://github.com/breadteleporter/alib
//
class alib {
const version = "0.0.2";
// Append JSON data to a file
function appendJSON($array, $file, $pretty=true){
try{
$tempArray = json_decode(file_get_contents($file));
array_push($tempArray, $array);
if ($pretty == false){$jsonData = json_encode($tempArray);}
if ($pretty == true){$jsonData = json_encode($tempArray, JSON_PRETTY_PRINT);}
file_put_contents($file, $jsonData);
return true;
} catch(Exception $e){
throw new Exception($e);
return false;
}
}
// Format a string into a tag
function formatInTag($string, $tag){
return "<" . $tag . ">" . $string . "</" . $tag . ">";
}
// Append a newline to a string
function formatWithNewline($string){
return $string . "\n";
}
// Append a HTML Linebreak to a string
function formatWithLinebreak($string){
return $string . "<br>";
}
// Get JSON data from a remote server or file
function getJSONAndDecode($location){
return json_decode(file_get_contents($location), true);
}
// Upload a file into a directory
function uploadToDir($file, $directory, $discardname=false, $newname=""){
if ($discardname == false){
$uploadfile = $directory . basename($file['name']);
} else{
$ext = end((explode(".", $file["name"])));
if ($newname == ""){
$uploadfile = $directory . uniqid() . "." . $ext;
} else{
$uploadfile = $directory . $newname . "." . $ext;
}
}
if (move_uploaded_file($file['tmp_name'], $uploadfile)) {
return true;
} else {
return false;
}
}
}