-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.php
71 lines (61 loc) · 2.47 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
session_start();
$message = '';
$file = ''; // Store the name of the uploaded file here
$error = []; // Store errors here
if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload')
{
if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK)
{
// get details of the uploaded file
$fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
$fileName = $_FILES['uploadedFile']['name'];
$fileSize = $_FILES['uploadedFile']['size'];
$fileType = $_FILES['uploadedFile']['type'];
$fileNameCmps = explode(".", $fileName);
$fileExtension = strtolower(end($fileNameCmps));
// sanitize file-name
// $newFileName = md5(time() . $fileName) . '.' . $fileExtension;
$newFileName = basename($fileName);
// check if file has one of the following extensions
// $allowedfileExtensions = array('jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc', 'mol', 'xyz');
$allowedfileExtensions = array('mol', 'xyz');
if ($fileSize > 5000000) {
$error[] = "File exceeds maximum size (5MB)";
}
if (in_array($fileExtension, $allowedfileExtensions))
{
// directory in which the uploaded file will be moved
$uploadFileDir = './upload/structures/';
$dest_path = $uploadFileDir . $newFileName;
// // Check if file already exists
// if (file_exists($dest_path)) {
// $error[] = "Sorry, file already exists.";
// }
// Continue if there is no error
if (empty($error)) {
if(move_uploaded_file($fileTmpPath, $dest_path))
{
$message = "$newFileName is successfully uploaded.";
$file = $newFileName;
}
else
{
$message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
}
}
}
else
{
$message = 'Upload failed. Allowed file types: ' . implode(',', $allowedfileExtensions);
}
}
else
{
$message = 'There is some error in the file upload. Please check the following error.<br>';
$message .= 'Error:' . $_FILES['uploadedFile']['error'];
}
}
$_SESSION['message'] = $message;
$_SESSION['file'] = $file;
header("Location: qrgenerator.php");