-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_book.php
81 lines (68 loc) · 1.95 KB
/
upload_book.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
72
73
74
75
76
77
78
79
80
81
<?php
// initialize session
ob_start();
session_start();
session_regenerate_id();
// include required file
require_once 'init.php';
//default response message
$response = array(
'status' => 0,
'message' => lang('error_uploading')
);
// validate session
if (!isset($_SESSION['UserEmail'])) {
$response['message'] = lang('no_session');
echo json_encode($response);
exit();
}
// validate param
if (
!filter_input(INPUT_POST, 'book_id', FILTER_VALIDATE_INT) ||
empty($_FILES['upload']['name']) ||
!is_uploaded_file($_FILES['upload']['tmp_name'])
) {
$response['message'] = lang('no_data');
echo json_encode($response);
exit();
}
$book_id = filter_input(INPUT_POST, 'book_id', FILTER_VALIDATE_INT);
// File upload path
$upload_dir = "upload/pdf/";
// File upload name
$file_name = basename($_FILES['upload']['name']);
// File upload path
$target_file = $upload_dir . $file_name;
//check if the file name is not repeated
if (file_exists($target_file)) {
$response['message'] = lang('file_exists');
echo json_encode($response);
exit();
}
//allowed file extension
$allow_types = array('pdf', 'doc', 'docx');
// get uploaded file's extension
$file_extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
// Check whether file type is valid
if (!in_array($file_extension, $allow_types)) {
$response['message'] = lang('not_allowed') . join(", ", $allow_types);
echo json_encode($response);
exit();
}
// Upload file to server
if (!move_uploaded_file($_FILES['upload']['tmp_name'], $target_file)) {
$response['message'] = lang('error_uploading');
echo json_encode($response);
exit();
}
//update DB path
$results = get_data(EDIT_FILE_PATH, [$file_name, $book_id]);
if ($results = 0) {
$response['message'] = lang('error_insert');
echo json_encode($response);
exit();
}
//new book inserted
$response['status'] = 1;
$response['message'] = lang('book_inserted');
echo json_encode($response);