This script provides validation of inputs of user publish-insertion form, including the attached files. More...
Functions | |
terminatePublishInsertionWithError ($errorParam) | |
terminateWithRemove ($errorParam, $mediaFiles, $avatarFile) | |
is_valid_number ($value, $min_value, $max_value) | |
is_valid_text ($value, $max_length) | |
is_valid_select ($value) | |
resizeImage ($sourceImage, $newWidth, $newHeight) | |
Variables | |
$make = $_POST['make'] ?? '' | |
$model = $_POST['model'] ?? '' | |
$short_description = $_POST['short_description'] ?? '' | |
$price = $_POST['price'] ?? '' | |
$year = $_POST['year'] ?? '' | |
$mileage = $_POST['mileage'] ?? '' | |
$power = $_POST['power'] ?? '' | |
$fuel = $_POST['fuel'] ?? '' | |
$engine_capacity = $_POST['engine_capacity'] ?? '' | |
$description = $_POST['description'] ?? '' | |
$number_fields_min_values | |
$number_fields_max_values | |
$text_fields_max_lengths | |
$isFormValid = true | |
if(! $isFormValid) if(empty($_FILES)) | $uploadDir = __DIR__ . '/media/' |
$avatar_uploadDir = __DIR__ . '/avatar/' | |
if(!is_dir( $uploadDir) &&!mkdir( $uploadDir, 0777, true)) if(!is_dir($avatar_uploadDir) &&!mkdir($avatar_uploadDir, 0777, true)) | $maxFileSize = 40 * 1024 * 1024 |
$allowedExtension = 'jpg' | |
$savedFiles = [] | |
$avatar_uniqueFileName = "filename" | |
for ( $i=1;$i<=5;$i++) | |
Includes the database connection file. | |
if(isset($error) && $error==="db_access_failed") | try |
$insertion_id = $pdo->lastInsertId() | |
Gets the last inserted ID for the insertions table. | |
for( $i=1;$i<=5;$i++) catch(PDOException $e) | $pdo = null |
Closes the database connection. | |
exit | |
< Redirects to the main page after successful insertion. | |
This script provides validation of inputs of user publish-insertion form, including the attached files.
Validates every field of the form. Resizes images and saves them in the local /media/ folder by newly generated names. Avatar is resized and saved separately by the newly generated name in the local /avatar/ folder. Creates new record in the 'insertions' table of the database and saves insertion data, including the address of the avatar. Addresses of the regular images are saved into the 'images' table by insertion_id and their order_number among the images of this particular insertion. Terminates the scripts, deletes all the recently-saved images of current insertion in /media/ and /avatar/ folders and redirects back to publish-insertion form with error context parameter and all the user inputs except files and values of select fields as parameters ofthe redirecting GET request in case of error of data validation or database communication.
is_valid_number | ( | $value, | |
$min_value, | |||
$max_value ) |
Validates if a number is within a specified range.
mixed | $value | The value to validate. |
int | $min_value | The minimum allowed value. |
int | $max_value | The maximum allowed value. |
is_valid_select | ( | $value | ) |
Validates if a selection is valid (not default or unset).
string | $value | The value to validate. |
is_valid_text | ( | $value, | |
$max_length ) |
Validates the length of a text field.
string | $value | The text value to validate. |
int | $max_length | The maximum allowed length. |
resizeImage | ( | $sourceImage, | |
$newWidth, | |||
$newHeight ) |
Resizes an image to the specified dimensions while maintaining aspect ratio.
resource | $sourceImage | The source image resource. |
int | $newWidth | The new width of the image. |
int | $newHeight | The new height of the image. |
Checks if there was a database access error. If so, terminates the operation and removes temporary files.
string | $errorCode | Error code to pass. |
array | $savedFiles | List of saved files to be removed. |
string | $avatar_uniqueFileName | Avatar file name to be removed. |
Handles PDO exceptions during insertion into insertions
table. Terminates the operation and removes temporary files.
Handles PDO exceptions during insertion into images
table. Terminates the operation and removes temporary files.
terminatePublishInsertionWithError | ( | $errorParam | ) |
Redirects to the publish-insertion page with error context parameter and all the user inputs except files and values of select fields as parameters ofthe redirecting GET request in case of error of data validation or database communication.
string | $errorParam | The error parameter to append to the query string. |
terminateWithRemove | ( | $errorParam, | |
$mediaFiles, | |||
$avatarFile ) |
Cleans up uploaded files and redirects with the specified error parameter.
string | $errorParam | The error parameter to append to the query string. |
array | $mediaFiles | The list of media files to remove. |
string | $avatarFile | The avatar file to remove. |
$allowedExtension = 'jpg' |
$avatar_uniqueFileName = "filename" |
$avatar_uploadDir = __DIR__ . '/avatar/' |
$description = $_POST['description'] ?? '' |
$engine_capacity = $_POST['engine_capacity'] ?? '' |
$fuel = $_POST['fuel'] ?? '' |
$insertion_id = $pdo->lastInsertId() |
Gets the last inserted ID for the insertions
table.
$isFormValid = true |
$make = $_POST['make'] ?? '' |
if(!is_dir($uploadDir) &&!mkdir($uploadDir, 0777, true)) if (!is_dir( $avatar_uploadDir) &&!mkdir( $avatar_uploadDir, 0777, true)) $maxFileSize = 40 * 1024 * 1024 |
$mileage = $_POST['mileage'] ?? '' |
$model = $_POST['model'] ?? '' |
$number_fields_max_values |
$number_fields_min_values |
$power = $_POST['power'] ?? '' |
$price = $_POST['price'] ?? '' |
$savedFiles = [] |
$short_description = $_POST['short_description'] ?? '' |
$text_fields_max_lengths |
$year = $_POST['year'] ?? '' |
exit |
< Redirects to the main page after successful insertion.
Terminates the script.
for($i=1; $i<=5; $i++) | ( | $i = 1; $i <= 5; $i++ | ) |
Includes the database connection file.
catch (PDOException $e) try |