-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
50 lines (39 loc) · 1.22 KB
/
upload.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
<?php
if (!isset($_FILES['myFile'])) {
die("There is no file to upload");
}
if (!isset($_POST)) {
die("There is no file data given");
}
$filePath = $_FILES['myFile']['tmp_name'];
$fileSize = filesize($filePath);
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
$fileType = finfo_file($fileInfo, $filePath);
if ($fileSize === 0) {
die("The file is empty");
}
if ($fileSize > 3145728) { //Check if file is greater than 3MB
die("The file is too large");
}
$allowedTypes = [
'image/png' => 'png',
'image/jpeg' => 'jpg'
];
if (!in_array($fileType, array_keys($allowedTypes))) {
die("Files of this type are not allowed");
}
//Name of the file..you can also use your preffered name here
if (isset($_POST['name'])) {
$fileName = htmlentities($_POST['name']);
}else{
$fileName = basename($filePath);
}
$extension = $allowedTypes[$fileType];
$targetDirectory = __DIR__ . "/file-uploads";//Directory where files are to be saved(__DIR__ is the current directory of this file)
$newFilePath = $targetDirectory . "/" . $fileName . "." . $extension;
if (!copy($filePath, $newFilePath)) { //copy the file, returns false incase of an error/failed
die("Could not move file");
}
unlink($filePath);//Delete the temp file
echo "File Uploaded Successfully";
?>