Skip to content

Student Listing of File Constants (with File Upload)

studentimagefileutil.php - Student Listing: Complete Code Listing

 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
require_once 'studentlistingfileconstants.php';

/**
 * Purpose:         Validates an uploaded student image file
 *
 * Description:     Validates an uploaded student image file is not greater than SL_MAX_FIE (1/2 MB),
 *                  and is either a jpg or png image type, and has no errors. If the image file
 *                  validates to these constraints, an error message containing an empty string is
 *                  returned. If there is an error, a string containing constraints the file failed
 *                  to validate to are returned.
 *
 * @return string   Empty if validation is successful, otherwise error string containing
 *                  constraints the image file failed to validate to.
 */
function validateStudentImageFile()
{
    $error_message = "";

    // Check for $_FILES being set and no errors.
    if (isset($_FILES) && $_FILES['student_image_file']['error'] == UPLOAD_ERR_OK)
    {
        // Check for uploaded file < Max file size AND an acceptable image type
        if ($_FILES['student_image_file']['size'] > SL_MAX_FILE_SIZE)
        {
            $error_message = "The student file image must be less than " . SL_MAX_FILE_SIZE . " Bytes";
        }

        $image_type = $_FILES['student_image_file']['type'];

        if ($image_type != 'image/jpg' && $image_type != 'image/jpeg' && $image_type != 'image/pjpeg'
            && $image_type != 'image/png' && $image_type != 'image/gif')
        {
            if (empty($error_message))
            {
                $error_message = "The student file image must be of type jpg, png, or gif.";
            }
            else
            {
                $error_message .= ", and be an image of type jpg, png, or gif.";
            }
        }
    }
    elseif (isset($_FILES) && $_FILES['student_image_file']['error'] != UPLOAD_ERR_NO_FILE
        && $_FILES['student_image_file']['error'] != UPLOAD_ERR_OK)
    {
        $error_message = "Error uploading student image file.";
    }

    return $error_message;
}

/**
 * Purpose:         Moves an uploaded student image file to the SL_UPLOAD_PATH (images/) folder and
 *                  return the path location.
 *
 * Description:     Moves an uploaded student image file from the temporary server location to the
 *                  SL_UPLOAD_PATH (images/) folder IF a student image file was uploaded and returns
 *                  the path location of the uploaded file by appending the file name to the
 *                  SL_UPLOAD_PATH (e.g. images/student_image.png). IF a student image file was NOT
 *                  uploaded, an empty string will be returned for the path.
 *
 * @return string   Path to student image file IF a file was uploaded AND moved to the SL_UPLOAD_PATH
 *                  (images/) folder, otherwise and empty string.
 */
function addStudentImageFileReturnPathLocation()
{
    $student_file_path = "";

    // Check for $_FILES being set and no errors.
    if (isset($_FILES) && $_FILES['student_image_file']['error'] == UPLOAD_ERR_OK)
    {
        $student_file_path = SL_UPLOAD_PATH . $_FILES['student_image_file']['name'];

        if (!move_uploaded_file($_FILES['student_image_file']['tmp_name'], $student_file_path))
        {
            $student_file_path = "";
        }
    }

    return $student_file_path;
}

/**
 * Purpose:         Removes a file given a path to that file.
 *
 * Description:     Removes the file referenced by $student_file_path. Supresses error
 *                  if file cannot be removed.
 *
 * @param $student_file_path
 */
function removeStudentImageFile($student_file_path)
{
    @unlink($student_file_path);
}