What are the common errors or issues that can arise when using the move_uploaded_file function in PHP for image uploads?

One common issue that can arise when using the move_uploaded_file function in PHP for image uploads is incorrect file permissions on the destination folder. To solve this issue, you need to ensure that the destination folder has the correct permissions set to allow the web server to write to it.

// Check if the file was uploaded successfully
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    $uploadDir = 'uploads/';
    
    // Check if the destination folder exists, if not create it
    if (!file_exists($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    // Move the uploaded file to the destination folder
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir . $_FILES['file']['name'])) {
        echo 'File uploaded successfully.';
    } else {
        echo 'Failed to move uploaded file.';
    }
} else {
    echo 'Error uploading file.';
}