How can PHP error reporting and logs be utilized to troubleshoot file upload issues effectively?

To troubleshoot file upload issues effectively using PHP error reporting and logs, you can enable error reporting to display any errors that may occur during the file upload process. Additionally, you can utilize PHP's built-in logging functions to log detailed information about the file upload process, such as file size, file type, and any errors that may occur.

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set up logging
ini_set('log_errors', 1);
ini_set('error_log', 'error.log');

// File upload handling
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    $uploadDir = 'uploads/';
    $uploadFile = $uploadDir . basename($_FILES['file']['name']);
    
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
        echo 'File uploaded successfully.';
    } else {
        error_log('File upload failed.');
    }
} else {
    error_log('File upload error: ' . $_FILES['file']['error']);
}