-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_image.php
55 lines (44 loc) · 1.93 KB
/
upload_image.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
<?php
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/classes/form/upload_image_form.php');
// Ensure this script is accessed within Moodle.
defined('MOODLE_INTERNAL') || die();
// Require the user to be logged in.
require_login();
$PAGE->set_url(new moodle_url('/auth/faceauth/upload_image.php'));
$PAGE->set_context(context_system::instance());
$PAGE->set_title(get_string('uploadimage', 'auth_faceauth'));
// Instantiate the form.
$form = new \auth_faceauth\form\upload_image_form();
// Process the form submission.
if ($form->is_submitted() && $form->is_validated()) {
$data = $form->get_data();
$file = $form->get_draft_file('face_image');
if ($file) {
// Directory to store enrolled face images.
$upload_dir = $CFG->dataroot . '/faceauth/faces/';
// Ensure the upload directory exists and is writable.
if (!is_dir($upload_dir)) {
if (!mkdir($upload_dir, 0755, true)) {
debugging("Failed to create upload directory: {$upload_dir}", DEBUG_DEVELOPER);
echo $OUTPUT->notification(get_string('dircreationsuccess', 'auth_faceauth'), 'notifyproblem');
exit;
}
}
// Move the uploaded file to the storage directory.
$file_extension = strtolower(pathinfo($file->get_filename(), PATHINFO_EXTENSION));
$upload_path = $upload_dir . $USER->username . '_' . time() . '.' . $file_extension;
$file->copy_content_to($upload_path);
// Display success message.
echo $OUTPUT->notification(get_string('uploadsuccess', 'auth_faceauth'), 'notifysuccess');
} else {
// Display error message.
echo $OUTPUT->notification(get_string('uploadfailed', 'auth_faceauth'), 'notifyproblem');
}
}
// Output the header.
echo $OUTPUT->header();
// Display the form.
$form->display();
// Output the footer.
echo $OUTPUT->footer();