How can absolute paths be used instead of relative paths in PHP to ensure the correct file locations are accessed during file uploads?

When uploading files in PHP, using absolute paths instead of relative paths ensures that the correct file locations are accessed consistently. Absolute paths provide the full directory path starting from the root directory, eliminating any ambiguity or dependency on the current working directory.

// Example of using absolute path for file upload
$uploadDir = '/var/www/html/uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
    echo "File is valid, and was successfully uploaded.";
} else {
    echo "Upload failed";
}