How can relative paths in PHP affect the success of file uploads?

Relative paths in PHP can affect the success of file uploads because they determine where the uploaded files are saved on the server. If the relative path is incorrect, the file may not be saved in the desired location or may not be saved at all. To ensure successful file uploads, it's important to use the correct relative path when specifying the destination folder for uploaded files.

// Set the upload directory using the correct relative path
$uploadDir = 'uploads/';

// Check if the directory exists, if not, create it
if (!file_exists($uploadDir) && !is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

// Specify the full path where the uploaded file will be saved
$uploadFile = $uploadDir . basename($_FILES['file']['name']);

// Move the uploaded file to the specified directory
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
    echo "File uploaded successfully!";
} else {
    echo "File upload failed.";
}