Files
Siro/walletintaleq.intaleq.xyz/v2/main/uploadImage.php
2026-06-11 18:22:59 +03:00

74 lines
2.6 KiB
PHP
Executable File

<?php
include "connect.php";
// Get the image file from the request.
$image_file = $_FILES['image'];
$driverID = filterRequest("driverID");
// Check if the image file was uploaded successfully.
if ($image_file['error'] != UPLOAD_ERR_OK) {
echo json_encode(array('status' => 'The image file was not uploaded successfully.'));
exit;
}
// Get the file name of the image file.
$image_name = $image_file['name'];
// Get the file extension of the image file.
$image_extension = pathinfo($image_name, PATHINFO_EXTENSION);
// Check if the image file is a valid image file.
if (!in_array($image_extension, array('jpg', 'jpeg', 'png'))) {
echo json_encode(array('status' => 'The image file is not a valid image file.'));
exit;
}
// Generate a new filename using the driver ID.
$new_filename = $driverID . '.' . $image_extension;
// Move the image file to the uploads directory with the new filename.
$target_dir = "card_image/";
$target_file = $target_dir . $new_filename;
move_uploaded_file($image_file['tmp_name'], $target_file);
// Update the image name variable with the new filename.
$image_name = $new_filename;
// Check if the driverID already exists in the database.
$sql = "SELECT * FROM card_images WHERE driverID = '$driverID'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// The driverID already exists in the database, so update the upload_date
$uploadDate = date("Y-m-d H:i:s");
$linkImage = 'https://ride.mobile-app.store/card_image/' . $image_name;
$updateSQL = "UPDATE card_images SET upload_date = '$uploadDate' WHERE driverID = '$driverID'";
mysqli_query($conn, $updateSQL);
if (mysqli_affected_rows($conn) > 0) {
// Print a success message for update
echo json_encode(array('status' => 'Record updated successfully'));
} else {
// Print a failure message for update
echo json_encode(array('status' => 'Failed to update record'));
}
} else {
// The driverID does not exist in the database, so insert a new row.
$linkImage = 'https://ride.mobile-app.store/card_image/' . $image_name;
$insertSQL = "INSERT INTO `card_images`( `driverID`, `image_name`, `link`) VALUES ('$driverID', '$image_name', '$linkImage')";
mysqli_query($conn, $insertSQL);
if (mysqli_affected_rows($conn) > 0) {
// Print a success message for insert
echo json_encode(array('status' => 'Record inserted successfully'));
} else {
// Print a failure message for insert
echo json_encode(array('status' => 'Failed to insert record'));
}
}
mysqli_close($conn);
?>